3

I have the small program

#include<iostream>
using namespace std;

class xyz{
   private: int xyz[];  // Why this line is not giving error.
}; 

int main(int argc, char *argv[])
{
    cout<<sizeof(xyz); //Q=Why this code is not giving error.
    return 0;
}

I am using gcc 4.3. Please tell me why am I wrong?

sehe
  • 374,641
  • 47
  • 450
  • 633
Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51

1 Answers1

3

What you are looking at is a g++ compiler extension. You can trigger the warning

ISO C++ forbids zero-size array 'xyz'

if you compile with the -Wpedantic flag, and you can stop it from compiling using the -pedantic-errors flag. The reason your output shows 0 is that g++ will translate that into (the also not standard conforming) int[0]. Also see this answer for more information.

Community
  • 1
  • 1
nikolas
  • 8,707
  • 9
  • 50
  • 70