c - Simple pass a pointer to another function FAILED -
i keep getting runtime error when trying pass input[i] function... don't know why. it's supposed pass address of input[i] pointer in other function don't know went wrong. must simple failed catch...
edit: program stop before passing array , exit.
void processinfixexp(const char * filename) { char ** input = null, ** output = null; int = 0, numinput = 0; char len; char tempinput[100] = {0}; file * pfile = null; if((pfile = fopen(filename, "r")) == null) { printf("cannot read file %s\n", filename); system("pause"); exit(1); } while(fgetc(pfile) == '\n') ++numinput; /* printf("%d\n", numinput); */ input = (char**)malloc(numinput * sizeof(char*)); output = (char**)malloc(numinput * sizeof(char*)); if(!input || !output) { printf("memory allocation failed.\n"); system("pause"); exit(1); } rewind(pfile); for(i = 0; fgets(tempinput, maxl, pfile) != null; ++i) { /* printf("%s\n", tempinput); */ len = strlen(tempinput); while(len && tempinput[len-1]) tempinput[len-1] = '\0'; input[i] = strdup(tempinput); printf("%s\n", input[i]); /* printf success */ } fclose(pfile); (i = 0; < numinput; ++i) { convertintopost (input[i], &output[i]); printf (" input[%2zu]: %-25s output[%2zu]: %s\n", i, input[i], i, output[i]); } free(input), free(output); } void convertintopost(char * in, char ** out) /* test passing input[] */ { printf("%s", in); /* no success */ }
it looks convertintopost
expected assign output[i]
indirecting through out
parameter receives. if haven't gotten around adding part of it, still need initialize pointer, printf
in caller succeed.
void convertintopost(char * in, char ** out) /* test passing input[] */ { printf("%s", in); /* no success */ *out = strdup(""); }
also, before free(input)
, free(output)
, need free strings point to, otherwise they'll orphaned.
for (i = 0; < numinputs; i++) { free(input[i]); free(output[i]); } free(input); free(output);
Comments
Post a Comment