linked list; why is it defined struct Node * next inside the struct?

simple question regarding a pointer declaration. why is struct required for the Node * next?

1
2
3
4
5
6
7
8
9
10
class dude
{
     int na;
     struct Node
     {
	int num;
	struct Node * next; // why the need for struct?
         Node * next; // why not just do this, (remove the word struct)?  they both seem to compile ok.
     };
};
There isn't a need, really. Prefixing the struct there I believe is a remnant from C.

-Albatross
thank god! i thought i was losing my mind!
closed account (Dy7SLyTq)
its actually one of the things i hated most about c. when i was writing this file api a while ago i was using c and was using enum to emulate file states, and it looks really clean in c++ because you can use the :: operator, but in c you have to define an object of the enum and set the state in it... its little things like that and function prototyping is the reason why i keep coming back to c++
It was because the names for structs were not in the same visible space as other variables and functions. Hence, you could actually name a struct and a function the same thing, for example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct point 
  {
  double x, y;
  };

struct point point( double x, double y )
  {
  struct point result;
  result.x = x;
  result.y = y;
  return result;
  }

struct point p = point( 10, 20 );

It kind of irked the C brevity gods, so it wasn't uncommon to see things like:

1
2
3
4
5
6
7
8
typedef struct node
  {
  ...
  struct node* next;
  }
node;

node* list = NULL;

C++ explicitly adds the struct namespace into the general namespace, but it still exists, so you can still play name games if you want.

:^]
Topic archived. No new replies allowed.