what is "struct* struct newStruct(int somedata) { } " syntax in c++

103 Views Asked by At

I was trying to make a tree in c++ and came across this code which is really confusing me.

struct node
{
    int data;
    struct node* left;
    struct node* right;
};

// The code bellow this is the part i dont understand
struct node* newNode(int idata)
{
    node* node = new struct node;
    node->data = idata;
    node->left = NULL;
    node->right = NULL;
    return node;
}

what is struct node* ? Some kind of a structure, but a pointer? Also shouldn't structs have ; at the end? For example, node has ; at the end of the body but not node* ?

1

There are 1 best solutions below

0
nvjsingh On

struct node* is the return type of function newNode() and not a definition in itself. The line you are referring to is the signature of the function newNode. It is followed by the function definition between the two curly braces {}. A function definition does not need a ; at the end.