the_it_crowd
the_it_crowd

Reputation: 59

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)
{
    // ...
}

Upvotes: 3

Views: 2887

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

The difference can be seen in the following code snippets.

The first one is

void function1( struct Node *start );

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

The second one is

void function2( Node *start );

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

The first code snippet is well-formed. In the parameter declaration of the function there is used an elaborated type specifier that introduces the type struct Node. The structure itself can be defined further after the function declaration.

For the second code snippet the compiler will issue an error because the name Node is not defined.

Also the name of the structure can be hiden by a declaration of a variable. In this case you also have to use the elaborated name of the structure.

For example

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

int Node;

void function1( struct Node *start );

From the C++ Standard (3.4.4 Elaborated type specifiers)

2 If the elaborated-type-specifier has no nested-name-specifier, and unless the elaborated-type-specifier appears in a declaration with the following form:

class-key attribute-specifier-seqopt identifier ;

the identifier is looked up according to 3.4.1 but ignoring any non-type names that have been declared....If the elaborated-type-specifier is introduced by the class-key and this lookup does not find a previously declared type-name, or if the elaborated-type-specifier appears in a declaration with the form:

class-key attribute-specifier-seqopt identifier ;

the elaborated-type-specifier is a declaration that introduces the class-name as described in 3.3.2

Upvotes: 2

Bathsheba
Bathsheba

Reputation: 234645

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

Upvotes: 5

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.

Upvotes: 5

Related Questions