tags:

views:

128

answers:

4

Furthermore, is there a difference between the initialization of the variables one and two, and the initialization of the varibles three and four? Background of the Question is, that i get an compiler error in Visual Studio 6.0 with the initialization of variable two and four. With Visual Studio 2008 it compiles well.

struct stTest
{
  int a;
  char b[10];
};

stTest one = {0};
stTest two = {};
stTest three[10] = {0};
stTest four[10] = {};
A: 

The initializations are the same. Visual Studio 2.0 is broken.


Edit:

Yes, they are initialized to zero.

Richard Pennington
+2  A: 

Yes, all of them a required to be initialized with 0 by the language standard (C++98).

Visual Studio 6 is known not to perform the proper handling of {} case: it doesn't even support {} syntax, if I remember correctly.

However, Visual Studio 6 is a pre-standard compiler. It was released before the C++98 standard came out.

AndreyT
A: 

(Ripped from MSDN)

If I have a structure called tPoint ...

    struct tPoint {   
        int x;   
        int y;
    };

... and I use it as follows ...

tPoint spot {10,20};

... I can expect that members x and y will be initialized.

As for your first question, I'd expect that the array b is not initialized because you only give one value for initialization.

You can initialize the values to zero by default:

struct stTest
{
  int a = 0;
  char b[10] = {0};
};

You can initialize the array like this:

char i[10] = {0};
stTest one = {0, i};

As for why it compiles with VS 2008 and not VS 6.0, VS 2008 probably ignores the empty set and doesn't try to initialize anything.

A. Walton
+1  A: 

See Michael Burr's answer to a similar question.

The short answer is yes, but a little emphasis sometimes helps, e.g.,

stTest s = {0};
Greg Bacon