How can I declare and define multiple variables in one line using C++?
I always though that if I declare these three variables that they will all have the value 0
int column, row, index = 0;
But I find that only index equals zero & the others are junk like 844553 & 2423445.
How can I initialise all these variables to zero without declaring each variable on a new line?
int column = 0, row = 0, index = 0;
When you declare:
int column, row, index = 0;
Only index is set to zero.
However you can do the following:
int column, row, index;
column = index = row = 0;
But personally I prefer the following which has been pointed out.
It's a more readable form in my view.
int column = 0, row = 0, index = 0;
or
int column = 0;
int row = 0;
int index = 0;
As @Josh said, the correct answer is:
int column = 0,
row = 0,
index = 0;
You'll need to watch out for the same thing with pointers. This:
int* a, b, c;
Is equivalent to:
int *a;
int b;
int c;