Friday, 27 September 2013

How to point to the expected char array after passing char* into a function in C?

How to point to the expected char array after passing char* into a
function in C?

Why I can not change the content the char* points to?
For example:
int main(int argc, char * argv[]) {
char *a = NULL; // now a = NULL
b(a);
// but now a points to NULL again! Why?
}
void b(char *argv[], char* c) {
// now a is passed in
c = *argv[3];
// now a actually points to the start of char array pointed to by argv[3]
}
From the output, I see the *a is passed to the function. Then inside the
function b, it actually pointed to the expected char[ ]. But when returned
from b, a became NULL again. Why? And how can a points to expected content
after return from b? Thank you!

No comments:

Post a Comment