Handling pointers within multiple functions in C -
i'm trying create functions out of existing code in order make cleaner, , i'm having problems:
it used be:
int foo(char * s, char * t, char ** out) { int val = strcmp(s, t); if (val == 0) { *out = strdup(s); return 1; } else { *out = strdup(t); return 5; } return 0; } now have:
int foo(char * s, char * t, char ** out) { somefunction(s, t, out); printf("%s", *out); return 0; } int somefunction(char *s, char * t, char **out) { int val = strcmp(s, t); if (val == 0) { *out = strdup(s); return 1; } else { *out = strdup(t); return 5; } return 0; } and i'm getting segmentation faults when try printf. should somefunction expecting *out? guess i'm still confused.
this code "correct" if understand intent. assume doing along lines of
char *s = "foo"; char *t = "bar"; char *out; foo(s, t, out); when want
char *s = "foo"; char *t = "bar"; char *out; foo(s, t, &out); // note & passes address of char* manipulated
Comments
Post a Comment