I have an array declared like so:
char *array[4];
int *new_array[2];
for (int i = 0; i < 2; i++){
strncpy(new_array[i], array[i], strlen(array[i]));
}
warning: passing argument 1 of ‘strncpy’ from incompatible pointer type [-Wincompatible-pointer-types]
note: expected ‘char * restrict’ but argument is of type ‘int *’
Your code is very wrong.
First it declares an array of integer pointers (hence the warnings) then those pointers aren't initialized. strncpy
isn't the proper function to call (and even if the memory was initialized, it wouldn't null-terminate your strings), you need strdup
which does the proper allocation & copy:
char *new_array[2];
for (int i = 0; i < 2; i++){
new_array[i] = strdup(array[i]);
}
(on some old systems, strdup
may not be available. If you're in that case, just use new_array[i] = malloc(strlen(array[i]+1));
then strcpy(new_array[i],array[i]);
)