What exactly the meaning of structure pointer

Viewed 64
struct Node
{
    int data;
    struct Node* left, *right;
};

What exactly the meaning of this struct Node *? Is the function below returning a pointer to a Node?

struct Node* newNode(int data)
{
    struct Node* node = new(struct Node);
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
1 Answers
struct Node;

Is the name of your struct.

What exactly the meaning of this struct Node*?

struct Node* node;

You're declaring a pointer to the struct Node that needs to be allocated with some memory. There is no new keyword in C. You need to use malloc() to allocate the required bytes of memory. You can allocate that like this:

struct Node* node = (struct Node *)malloc(sizeof(struct Node));

Is it returning a pointer to a Node?

This line:

return (node);

Returns the type of struct Node* to the function.

Related