Passing string variables
Could someone pls tell me what I am doing wrong in the code below.
I'm trying to read two strings into two variables.
When I printf in the function ProcessString, I get what I am looking for. However when I printf first and second in
main, I don't get those strings.
I'm I calling ProcessString the wrong way from main?
void ProcessString(FILE *infile, char *fval, char *sval)
`int i = 0;
if ((ch = fgetc(infile)) != EOF)
while (ch != 'X')
localfval[i++] = ch;
localfval[--i] = '\0';
i = 0;
while (ch != 'Y')
localsval[i++] = ch;
localsval[--i] = '\0';
fval = localfval;
sval = localsval;
printf("%s, %s\n", localfval, localsval);
if (infile = fopen("filename.txt", "r") == NULL)
ProcessString(infile, first, second);
printf("The first string is %s\n", first);
printf("The second string is %s\n", second);
You're pointing at local variables, which are destroyed as soon as the function call ends. You need to allocate space with malloc, or some such, or pass it arrays, and strcpy the strings to whatever you pass it. Oh, and if you want to make a pointer point to some space you allocate inside the function, you'll have to use a pointer to a pointer.
Thanks Quzah for your help. Unfortunately, I'm not that versed in C programming so could you please elaborate further? Maybe with a couple of lines of code.
The problem with this is your calling function doesn't know the size of your buffers, so you'll just assume you have enough. This may or may not be a problem. Or, you can pass another argument or so, telling them the size of them.
int main( void )
char buf1[ BUFSIZ ], buf2[ BUFSIZ ];
yourfunction( buf1, buf2 ); /* pass two arrays to use */
Use dynamic allocation:
Here you allocate memory inside your function, and update the pointers so that outside the function you're still pointing at the right spot. The downside to this is that you have to make sure you free your allocated stuff correctly. You'll also have to check outside to make sure you've been given some memory (make sure your pointers aren't NULL after the function's been called) when the function ends.
int main( void )
char *buf1, *buf2;
yourfunction( &buf1, &buf2 ); /* pass pointers to the pointers, allocate and fill */
The first version:
Here you're assuming you're being passed an array and you're allowed to fill it. You can't be passing string literals, or anything else that's read only.
void yourfunction( char buf1, char buf2 )
...work with them directly...
The second version:
You'll need to test the return value (ie: your pointer) to make sure malloc didn't return NULL. If it did, it means you're out of memory, so don't try to use what it returned. When you're all done, you have to free whatever you've allocated. So call free on the pointers (outside the function) when you're done with them, once each.
void yourfunction( char **buf1, char **buf2 )
*buf1 = malloc( somesize );
*buf2 = malloc( somesize );
...now work with them, assuming they're not null...
You have to use a pointer to a pointer, because everything in C is 'pass by value'. Thus, if you make a pointer like you had it origionally, you are in effect making a copy of the pointer, so any changes are lost, because you're not really working with the origional one.
There's a FAQ or two on dynamic memory allocation I believe, in the FAQ section.
Or use the same interface which fgets() uses, which amounts to
function ( char *storeTheAnswerHere, int maxLengthOfAnswer );
This makes the entire problem of how much space to allocate (whether an array or via malloc) the responsibility of the caller. The called function just does it's job with the resources provided.