First of all, you really shouldn't be printing the pointer as a character string. Use the %p specifier, or similar. Anyway, the issue is that in C all things are passed to functions by value - pointers included. So, from main, the pointer itself (the variable) is located at address 0xdeadbeef and contains the value of 0. When you pass it to the function, the *value* of the pointer (eg: 0) is copied, but the address of the local variable within the function is *not* 0xdeadbeef, so the changes made to it will obviously not be reflected in the variable within main. Hope that makes sense. Anyway, the solution is simply one more level of indirection - in other words, pass the *address* of the pointer:
Code:
#include <stdlib.h>
#include <stdio.h>
struct node {
int i;
char *s;
};
void f1(struct node **ptr) {
(*ptr) = malloc(sizeof(struct node));
(*ptr)->i = 2;
printf("%p\n", ptr);
}
int main() {
struct node *ptr=NULL;
f1(&ptr);
printf("%p\n", &ptr);
return 0;
}