I want to create an array of strings in C. I want to use a pointer to char pointer (char **).
The no. of strings will known at runtime only. The length of each strings is also known at runtime only. How can i go about it?
I tried the following
//Create a zagged array of strings based on user input
//USE C ONLY
printf("\n Enter the number of strings : ");
if(!(str_array = (char**)malloc(n*sizeof(char*))))
printf("incorrect memory allocation");
str_array[i]=(char*)malloc((strlen(temp))*sizeof(char)); //str_array[i] = "string i" works
but somewhere segmentation fault occurs.
Within the while loop if i use
str_array[i] = "some stirng"
i.e., hard coding the strings, it works. But it doesn;t work for user input. I think i am not able to pass the addresses correctly. Can some one help me do it. A little explanaation will be greatly appreciated.
please do not give me examples with integer pointers. I know the solution for them. I am having trouble in understanding how the char* pointer to array works.