3

I have Struct like these

 typedef struct sample
 {
       double x,
       double y,
       double z
 }s1;
 s1 s;

will the content in s variable initialized or not?

What will be the values of x,y,z?

thanks

karthik
  • 17,453
  • 70
  • 78
  • 122
  • 3
    Note that, in C++, you don't often see `typedef struct foo { ...`. This is because in C++, unlike C, all structs names are also type names. In C++ you can write: `struct sample {}; sample s;`. – Robᵩ Apr 27 '11 at 16:39

4 Answers4

9

x, y and z won't be initialized if s is defined in a function scope. They would be containing some unspecified values. At file scope the data members would be initialized to their default values.

In C++ however you can have a constructor initializer list to initialize the data members

For example

struct ABC
{
   int x;
   int y;

   ABC(): x(1),y(2){}
};

ABC s; // x and y initialized to 1 and 2 respectively

In C++ you also have default initialization and value initialization to initialize data members.

Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
4

In the code you presented, the fields will be uninitialized. You can add a constructor (if you need/can), or in case you need the POD-ness (some part of your code depends on some of those properties) and you cannot add a constructor, you can still value-initialize the struct (i.e. set each member to 0) at the place of use:

struct sample        // typedef not required
{
   double x,
   double y,
   double z
};
sample s = sample(); // will set all members to 0.0

Now, if you want to initialize different members with some particular values, because it is an aggregate you can use aggregate initialization:

sample s = { 1.0, 3.0 };

That will set x to 1.0, y to 3.0. Since there is no value for z, the compiler will set it to 0.0. Note that this means that sample s = {}; is equivalent to sample s = sample();

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • See also [this question](http://stackoverflow.com/questions/4932781/why-is-a-pod-in-a-struct-zero-initialized-by-an-implicit-constructor-when-creatin) for more detailed description of default vs. value initalization. – Jan Hudec Apr 27 '11 at 07:19
2

If it is C++, you could make constructor.

struct s1
{
  s1( const double x = 0.0, const double y = 0.0, const double z = 0.0 )
  : x(x), y(y), z(z)
  {
  };
  double x;
  double y;
  double z;
};

s1 s;
Naszta
  • 7,560
  • 2
  • 33
  • 49
  • But you don't have to make a constructor and still can zero-initialize them. – Jan Hudec Apr 27 '11 at 07:11
  • You should make that constructor `explicit`, any constructor that can be called with a single argument is prone to open up the system for unwanted conversions: `void foo( s1 const & ); foo( x );` If `x` is of arithmetic type, that code is probably an error, but will actually work by converting `x` to `s1` as `s1( x )` and then calling `foo`. It is usually safer to make the constructor explicit, and if the code above was intentional the user would have to explicitly request the conversion: `foo( s1(x) );` which is not too much of a burden. – David Rodríguez - dribeas Apr 27 '11 at 07:19
1

Built-in types like double and int are initialised if the variable is static or at namespace/file scope, otherwise - for efficiency reasons - they're not initialised unless a constructor indicates that's useful.

Note: this answer addresses the "s1 s;" situation you describe. It is possible to provide an explicit initialisation when defining the variable, but that's another case.

To add a constructor so:

struct X
{
    X() : x_(0), y_(0), z_(0) { }
    double x, y, z;
};
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252