I have this struct:
struct Snapshot
{
double x;
int y;
};
I want x and y to be 0. Will they be 0 by default or do I have to do:
Snapshot s = {0,0};
What are the other ways to zero out the structure?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They are not null if you don’t initialize the struct.
The second will make all members zero, the first leaves them at unspecified values. Note that it is recursive:
The second will make
p.s.{x,y}zero. You cannot use these aggregate initializer lists if you’ve got constructors in your struct. If that is the case, you will have to add proper initalization to those constructorsWill initialize both x and y to 0. Note that you can use
x(), y()to initialize them disregarding of their type: That’s then value initialization, and usually yields a proper initial value (0 for int, 0.0 for double, calling the default constructor for user defined types that have user declared constructors, …). This is important especially if your struct is a template.