As a beginner with C++ I was learning about linked list and other data structures. After looking at a few implementations online I found these two ways they defined struct. What is the difference between the two. In one we add "struct" before the next pointer, and in one we don't.
Way 1:
struct node
{
int data;
node *next;
};
Way 2:
struct node
{
int data;
struct node *next;
};
struct node *next;
Is only necessary in C code. In C, doing:
node *next;
Is not allowed. However, in C++, you can use both methods. There is no difference between them in this case.
The only time you would need the struct
keyword in C++ for a declaration would be if there is some sort of ambiguity or if the struct
has not been defined yet. (An example for the first would the stat
function, which is a function and a struct
on POSIX systems).
What is the difference between the two.
The difference is that the latter (called elaborated type specifier ) declares that node
is a struct.
In this case, it is redundant to declare that node
is a struct, because that has already been declared:
struct node
^^^^^^^^^^^ <-- node is a struct
{
int data;
node *next;
};
An example of a case where there is a difference:
struct a {
//b* pointer; // oops, we don't know what b is
struct b* pointer; // OK, b is a struct
};
struct b{};
It is typically † not absolutely necessary to use an elaborated type specifier, as it is possible to use a separate declaration instead:
struct b;
struct a {
b* pointer;
};
struct b{};
Choice between them is personal preference.
† I say typically because sometimes you need it to disambiguate a variable of same name from a type:
int foo;
struct foo{};
int main()
{
foo = 10; // refers to int
struct foo instance; // refers to the struct
}
It is usually a bad design to use same name for multiple things, so this is not very typical.
Finally, it is sometimes desirable to write header files that can be included in both C and C++. Such header must use the common subset of both languages.
In C, node
is not a type name, but a tag for a structure. It is necessary in C to refer to a struct tag using struct
keyword.
The second version is required in C, but bad style in C++, and there are almost no situations in C++ in which you actually have to add the keyword like this.
Note that you don't have to write your own linked-list implementations in C++, unless it's for academic learning purposes. Use std::list
or std::forward_list
.
In your case there is no difference at all. However, consider this example:
struct foo { struct node* f; }; // <- this is fine
struct foo_broken { node* f; }; // <- wont compile
struct node { };
int main() {
foo f;
//foo_broken g;
}
Placing the keyword struct
allows you to declare the member before you declare node
. Without struct
the compiler will issue an error (for foo_broken
):
error: ‘node’ does not name a type
That is, placing the keyword struct
acts like a forward declaration. To use foo
you need to supply a declaration of node
but it can come after the declaration of foo
.
This is because C++ grew out of C and C's syntax, and the language kept both types of syntax even though the explicit use of struct
to declare the type of a variable was no longer needed. There are some cases, ie, to resolve ambiguity between different types/functions/etc. with the same name, where it would make a difference. Also by keeping this syntax valid in C++, it would help people port C code over to C++ without introducing an error.
In C, by default you had to always place the struct
keyword in front of the name of the struct. So only Way #2 was valid C:
struct node
{
int data;
node *next; //error in C!
};
struct node
{
int data;
struct node *next; //compiles
};
But you would often see in C code that struct
declarations were paired typedef
s in order to create new types, such as:
typedef struct node_t
{
int data;
struct node *next;
} node;
And this would allow node
to be used as a type without struct
in front of it. In C++, it effectively automated this typedef
for all struct
and class
types in the code, which is why you can use the struct
or class
name without the appropriate qualifier before it.
C ++中的两个声明没有区别
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.