So I have a program that works sometimes, but other times it doesn't. Sometimes putting in a printf statement will make the program magically work (and sometimes a printf will make it break). This makes me think I messed up as far as pointers/allocation goes, as I still have trouble wrapping my head around some things. The program is too long to post all the code, so I'll just show where I think the problem is.
I was given a function that takes a FILE* and a few char** as parameters, returning an int. The function is parsing a file, storing necessary strings to where the double pointers point to.
Here is how I used the function (alot of code is omitted/simplified):
char **s1;
char **s2;
char **s3;
int main(int argc, char ** argv){
s1 = (char**)malloc(20);
s2 = (char**)malloc(20);
s3 = (char**)malloc(20);
/* Unnecessary code omitted */
read(inFile);
}
int read(FILE* in){
/* omitted code */
parse(in,s1,s2,s3);
printf("%s",*s1); /* This is to show how I access the strings */
}
Im pretty sure that somewhere in my program, those strings are getting overwritten because I didn't allocate the memory properly. Hopefully my mistake is visible in the code snippet I gave, because I don't have many other theories for why my code doesn't work