I have a pointer to a character array:
space=(char**)calloc(100000,sizeof(char*));
for(i=0;i<100000;i++){
space[i]=(char*)calloc(1,sizeof(char));
}
such that when I use the following command
printf("%s\n",space[0]);
I get "a b c d e"
I want to assign "a b c d e" to
char c[10];
such that
printf("%s",c) yields
"a b c d e"
but when I try
c=space[0]
I get the following error:
incompatible types in assignment
What am I doing wrong?
First, you need to allocate enough space to hold the entire string at location space[0]. Currently you are allocating a single character.
After you do that you would use strcpy() to copy the string into the newly allocated buffer.
PS: Don’t forget to free() any prior allocated strings at space[0] (like the one you create in the for() loop) before you allocate for the new string. Or you could use realloc() instead.