Take the 2-minute tour ×

This function needs to be passed a pointer to a pointer.

void get_name(person** p) {
    puts(p->name); // this is probably wrong

This is how I call the function (I'm not sure about this either):

int main() {

    get_name(&person); // is this wrong?

    return 0;

Person is obviously a struct with a name property which is a string.

How do you refer to the value *p points to from **p?

share|improve this question
The proper return type of main is int, not void. –  James McNellis Dec 17 '10 at 22:27
I know that, but here I wanted to spare the "return 0;" at the end, I changed it. –  Gal Dec 17 '10 at 22:29
add comment

1 Answer

up vote 8 down vote accepted

x->y is just a shortcut for (*x).y. * performs indirection (that is, it gives you the thing pointed to by the pointer), so you need to perform indirection twice to get the thing pointed to the pointer that is pointed to by the pointer:


If person is the name of a typedefed struct, then get_name(&person) is not correct; you need an instance of that struct and you need a pointer to that instance that you can pass to the function:

int main() {
    person p;
    person* pp = &p;

However, it's not exactly clear why get_name needs to take a person**; presumably a person* would be sufficient.

share|improve this answer
yep this works. –  Gal Dec 17 '10 at 22:30
add comment

Your Answer


