User arrays:
Code:
int main( void )
{
char buf1[ BUFSIZ ], buf2[ BUFSIZ ];
yourfunction( buf1, buf2 ); /* pass two arrays to use */
...do stuff...
return 0;
}
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.
Use dynamic allocation:
Code:
int main( void )
{
char *buf1, *buf2;
yourfunction( &buf1, &buf2 ); /* pass pointers to the pointers, allocate and fill */
...do stuff...
return 0;
}
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.
The first version:
Code:
void yourfunction( char buf1[], char buf2[] )
{
...work with them directly...
}
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.
The second version:
Code:
void yourfunction( char **buf1, char **buf2 )
{
*buf1 = malloc( somesize );
*buf2 = malloc( somesize );
...now work with them, assuming they're not null...
}
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.
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.
Quzah.