Hi guys,
Iam struck with 2D char array! Hope, you could help on this.
My requirement is, I want to add the string in string array and I dont know the size at compile time. Hence, have to use dynamic mem allocation.
Here is the code what iam trying..
Code:void main(void) { char **name, string[100] = "Xyz String"; int i=0, n=25, index=0, numOfRows=1; name = (char **)calloc(numOfRows, sizeof(char **)); for(i=0; i<n; i++) AddStr(name, &index, string); for(i=0; i<index; i++) printf("--->%03d %s\n",i,name[i]); for(i=0; i<index; i++) free(name[i]); free(name); } int AddStr(char **name, int *index, char *str) { int i=0; //reallocating the mem for 2D row size as its incrementing every time. // NOT Sure, is this required or correct? name = (char **)realloc(name, ((*index)+1) * sizeof(char **)); //allocating the mem for column for every row name[*index] = (char *) calloc(strlen(str)+1, sizeof(char)); strcpy(name[(*index)++],str); } Pls. help to to solve this!



LinkBack URL
About LinkBacks


