-4
struct node
{
  int data;
  struct node *next;
}

cout<<sizeof(struct node)<<sizeof(node)<<endl; 
//no error, C++ allows us to use sizeof(struct node) and sizeof(node) both. 

Whereas we cannot do the same with int datatype

int a;
cout<<sizeof(int) <<sizeof(a) <<endl;//there is no error here

  //BUT

cout<<sizeof(int a) <<endl;//this throws an error 

I understand that "struct node" itself is like a datatype which can be used to declare variable of type "struct node". Going by the behavior of how sizeof() works with int, it is understandable that sizeof(struct node) is equivalent to sizeof(datatype) and hence is correct usage.

But how does sizeof(node) work as well ? It does not throw any error. "node" in itself cannot be used to declare any other variables, it needs to be "struct node" to declare a variable.

Qrious
  • 5
  • 1
  • 6
    You are not doing the same thing. –  May 05 '17 at 10:46
  • How does sizeof(node) work ? Shouldn't it error. Just node as such is not really useful unless I use some typedef, which I haven't. – Qrious May 05 '17 at 10:49
  • @Qrious Try `sizeof(struct node node_instance)`. That's more simmilar to what you're doing with `int`. – LogicStuff May 05 '17 at 10:50
  • 1
    <>: says who? https://ideone.com/Z0Z0dC – Rene May 05 '17 at 10:51
  • @LogicStuff Yes, I get your point. But my question is mainly to understand how sizeof(node) works without any error. "node" alone has no significance right ? I have not declared any variable of type node. If I had declared a variable of type node using struct node node1 and used sizeof(node1), I understand that this is correct usage. But just using sizeof(node) should have errored. – Qrious May 05 '17 at 10:54
  • @Rene now I see why sizeof(node) works just fine.. Thanks ! – Qrious May 05 '17 at 10:57
  • @Rene, if we do not need to use struct node to declare a variable and using just "node" works fine while declaring a variable, then what is the use of typedef ? Example: struct node { int a; struct node *next; }; struct node node1; node node2; //Both these above declarations work fine. That means we do not need typedef while declaring struct variables ? – Qrious May 10 '17 at 12:56
  • @Qrious Right, in C++ you don't need the typedef: http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c – Rene May 10 '17 at 14:51

1 Answers1

5

In C++, a class type declared as struct A {/* blah blah blah */}; or class A {/* blah blah blah*/}; can be referred to as a type later as class A or struct A or simply A. These types can be passed into the sizeof operator.

Variables can also be passed into the sizeof operator, like so:

int a;
long b;
node c;

std::cout << sizeof(a) << ' ' << sizeof(b) << ' ' << sizeof(node) << '\n';

But a variable declaration like int a cannot be put into the sizeof operator:

std::cout << sizeof(int a); // Compile error

Similarly, a declaration involving a class type cannot be put into the sizeof operator:

std::cout << sizeof(node c); // Compile error
  • Understood, thank you ! Thanks to @Rene also for pointing it out – Qrious May 05 '17 at 10:59
  • "In C++, a class type declared as `struct A` or `class A`" +100 right there. Congratulations for spreading the correct notion that `struct` declares a class type in C++. – bolov May 05 '17 at 11:04