Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between struct Node* and Node* in function call?

What is the difference between what is being passed to function1 and function2?

struct Node
{
    int data;
    struct Node *next;
};

void function1(struct Node *start)
{
    // ...
}

void function2(Node *start)
{
    // ...
}
like image 534
the_it_crowd Avatar asked Aug 22 '26 22:08

the_it_crowd


2 Answers

The difference essentially, is in what happens when the type wasn't previously declared at the point of the function declaration/definition.

So long as function1 doesn't try to access any member of the object, it doesn't need the type definition or declaration to be present. It has the effect of introducing a declaration of struct Node into the enclosing scope.

function2 does need it (or a forward declaration somewhere to exist), regardless of what it does with the object. It does not implicitly introduce the class type.

like image 130
StoryTeller - Unslander Monica Avatar answered Aug 25 '26 13:08

StoryTeller - Unslander Monica


Aside from potentially standing in as a forward declaration (a moot point in your case), there is no difference at all. Your using struct Node* in the function parameter list is paying homage to requirements in C.

You don't need to do that in C++, since it has a different way of organising its namespaces to C.

For a more formal explanation of the construct, see http://en.cppreference.com/w/cpp/language/elaborated_type_specifier

like image 33
Bathsheba Avatar answered Aug 25 '26 13:08

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!