DaemonOfTheWest DaemonOfTheWest - 1 month ago 5
C Question

Will declaring as pointer with a function that returns a pointer cause a chain of two pointers?

If I declare a pointer with a function that returns a pointer, will the pointer I declared be identical to the pointer the function returns, or will it be a pointer to the pointer that the function returns?

Example:

typedef struct foo{
int n;
} foo;

// this function returns a pointer to a structure foo
foo* returnPointer(){
foo* tmp = malloc(sizeof(foo));
tmp->n = 1;
return tmp;
}

// if I do this, will abc be a pointer to a foo structure (the desired effect)
// or will it be a pointer to another pointer to a foo structure?
foo* abc = returnPointer();

Answer

abc will be what you declare it to be. A pointer to a foo struct. The function also returns a foo * (pointer to a foo struct). So there is type agreement.

edit: Also as @phaazon stated, the function has mistakes (malloc is needed).

Comments