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)
{
// ...
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With