C: Passing a pointer down, updated pointer can't be accessed from calling functions! -
i've got problem- assign pointer variable in called function, when trying access updated pointer in calling functions, doesnt show updated, , results in seg fault. help!
"bottommost function":
void comparestrings(char* firstfrag, char* secondfrag, int* longestcommonlength, char* suffix, char* prefix, int* relative_pos) { suffix = firstfrag; prefix = secondfrag; }
it called function (the printf triggers fault)
int findbestoverlap(char* fragmentarray[], int fragmentcount, int* longestcommonlength, char* suffix, char* prefix, int* relative_pos) { comparestrings(fragmentarray[firstfrag], fragmentarray[secondfrag], longestcommonlength, suffix, prefix, relative_pos); printf("lcl: %i || prefix: %s || suffix: %s", *longestcommonlength, prefix, suffix); }
the variables prefix , suffix in turned created in higher calling function, processstrings.
void processstrings(char* fragmentarray[], int fragmentcount) { int longestcommonlength = 0; char* suffix; char* prefix; int relative_pos; // first letter of suffix is, vis-a-vis prefix if ((findbestoverlap(fragmentarray, fragmentcount, &longestcommonlength, suffix, prefix, &relative_pos)) != 0) { } }
help!
you're not updating pointer, you're changing local value of argument. need use pointer pointer (e.g. char**
) , change pointed-to value instead.
void comparestrings(char* firstfrag, char* secondfrag, int* longestcommonlength, char** suffix, char** prefix, int* relative_pos) { *suffix = firstfrag; *prefix = secondfrag; }
then need dereference appropriately in caller. have fun!
Comments
Post a Comment