I need to allocate memory dynamically for an array of pointers.
Let us assume,
char *names[50];
char *element;
I used the following code to allocate memory dynamically which is producing error.
names=malloc(sizeof(char *));
Afterwards, i need to assign another character pointer to this one, say
names=element;
I am getting error as ": warning: assignment from incompatible pointer type".
How can i resolve this?
Will allocate either 4 or 8 bytes (depending on your system). This doesn’t make sense since your array was already sized at 50 entries in the declaration…
This is not how arrays are used in C. You have declared that there are 50 elements in “names” and each one is allocated as a different pointer to an array of characters. You need to decide which element in the array you want to assign. For eaxample: