0

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] = {};
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
  • This all should be valid syntax. What is the nature of the compiler error and can you provide an example that wraps this snippet in a short `main()` that still causes the error? – Adam Dec 22 '09 at 22:26
  • Visual Studio 6.0 had serious problems in conforming to the standards. Microsoft has done a lot of work, and VS 2008 is very close to the standard, although there's a few minor issues left. – David Thornley Dec 22 '09 at 22:27
  • Visual Studio 6 had no real standards to conform to. It was released before the first C++ standard came out. – AnT stands with Russia Dec 22 '09 at 22:28

4 Answers4

2

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

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};
Community
  • 1
  • 1
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
0

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


Edit:

Yes, they are initialized to zero.

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
0

(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
  • 7
  • 2