Passing pointers to functions means that you can alter the data that it points to, fine!
Just don't expect to re-assign this pointer in a function and
expect it to be pointing there when you return from the function.
That's because even though you can alter the data, you cannot
alter where the pointer is pointing to.
Reason being that a copy of the pointer is passed to the function.
Eg.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func(char* name)
{
name = 0;
}
int main()
{
char *name = (char*)malloc(30);
strcpy(name,"TEST");
printf("Before func() : %s\n", name);
func(name);
printf("After func() : %s\n", name);
free(name);
return 0;
}
If you want to alter the pointer then do it this way:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func(char** name)
{
*name = 0;
}
int main()
{
char *name = (char*)malloc(30);
strcpy(name,"TEST");
printf("Before func() : %s\n", name);
func(&name);
printf("After func() : %s\n", name);
free(name);
return 0;
}
Just something good to know!