Tommy Saechao Tommy Saechao - 4 years ago 139
C Question

What's the difference: Creating structure pointer inside a structure of the same type with the keyword struct

Coming from a C++ background here. I'm required to write C for my one of my courses, which I've never been exposed to. What's' the difference beteween these two declarations? Why include the struct keyword? Are there different meanings? Do they have different meanings in C++ vs C?

struct A {
int dataA;
A* nextA;
};

struct B {
int dataB;
struct B* nextB;
};


The comments below say that the first is invalid in C. However, I'm compiling just fine with gcc.

enter image description here

Edit: I was compiling a .cpp file with gcc. I'm such a noob lol.

Solution: The the second is required for C where the first is only valid in C++.

Answer Source
struct B {
    int dataB;
    B* nextB;
};

is the same as

struct B {
    int dataB;
    struct B* nextB;
};

in C++ but not in C. When using C, you'll have to use the second form. The first form is not legal C.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download