DCR
DCR

Reputation: 15665

confused on how to pass pointer

typedef struct nodeWords
{
    char * word;
    struct nodeWords *left;
    struct nodeWords *right;
} nodeWords;

void leftRightAddress(int buckets, nodeWords *nodeHash);


int main(void)
{
    nodeWords *nodeHash = malloc(500 * sizeof(nodeWords));

    
    leftRightAddress(500, nodeHash);

}

void leftRightAddress(int buckets, nodeWords *nodeHash)
{

}

can some explain why when I call the function leftRightAddress() in main I need to use the argument nodeHash and not the pointer *nodeHash? The function is defined as taking a pointer.

Upvotes: 1

Views: 42

Answers (2)

ikegami
ikegami

Reputation: 385657

Taking a pointer and getting what it points to is a dereference, and *nodeHash is a dereference.

The value of nodeHash is a struct nodeWords *, i.e. a pointer.

The value of *nodeHash is a struct nodeWords, i.e. a struct.

The parameter's type is struct nodeWords *, so you want to pass the value of nodeHash (the pointer), not *nodeHash (the struct).

Upvotes: 0

Luke Lewis
Luke Lewis

Reputation: 110

Your declaration of nodeHash makes it a pointer type (i.e. points to a location in memory). If you then pass *nodeHash, you are essentially dereferencing it, so what you're actually passing then is the object it is pointing to in memory.

nodeWords *nodeHash //nodeHash = pointer to nodeWords object
someFunction(*nodeHash) //passing nodeWords object
someFunction(nodeHash) //passing pointer

Upvotes: 2

Related Questions