C - Storing a char array multiple times in a 2d char* array -


the problem here whenever change contents of studname contents inside studarr change too.

if input looks (aaa,bbb,ccc) first store aaa inside studname , store studname studarr.

i'm trying make:

studarr[0][1] = "aaa"  studarr[0][2] = "bbb"  studarr[0][3] = "ccc 

but when use code of them equal ccc. there way can fix this?

for (j = 0; j < numcourses + 1; j++){     = 0;     k = 0;     while ((c = fgetc(ifp)) != ')'){         if (c == ','){             studname[3] = '\0'; // ends sting null char             studarr[j][k+1] = studname;             k++;             = 0;         }         else{             studname[i] = c;             i++;         }     }     studname[3] = '\0'; // ends sting null char     studarr[j][k+1] = studname; // store studname in studarr  } 

with assignment:

studarr[j][k+1] = studname;

you store pointer char[] studname. should allocate memory every instance, here:

studarr[j][k+1] = strdup(studname); 

note: remember free allocated memory.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -