How do I initialise all these variables to zero without declaring each variable on a new line?
int column, row, index = 0; int column = 0, row = 0, index = 0; With the following declaration, only the last variable (index) is set to 0:
int column, row, index = 0; Instead, the following sets all variables to 0:
int column, row, index; column = index = row = 0; But personally, I find the following methods much more readable:
int column = 0, row = 0, index = 0; int column = 0; int row = 0; int index = 0; bool previousInputValue, presentInputValue;) or if they just happen to be the same type now but don't really need to be (uint8_t height, width; might turn into uint8_t height; uint16_t width; in the future and should have been uint8_t height; uint8_t width; to begin with).uint8_t height; uint16_t width; instead of uint8_t height, width; saves 10 characters in the future. :-) you can of course do it however you like. Just make sure you make it easily read. So the last form is the most explicit.int presentValue = 0; typeof(presentValue) previousValue = presentValue;, but I believe that typeof() is a non-standard GCC extension.decltype as a portable version of GNU C typeof. So decltype (width) height = 0; works, but requires more mental effort to read.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; unsigned long x, y; declared x as unsigned long but y as just unsigned, aka unsigned int! That's exactly the same! </rant>int *a means that *a represents an int value.void* a will compile and void *a, b, c won't. This rationalization works for me.int column(0), row(0), index(0); Note that this form will work with custom types too, especially when their constructors take more than one argument.
auto...): int column { 0 } , row { 0 } , index { 0 } ;As of C++17, you can use Structured Bindings:
#include <iostream> #include <tuple> int main () { auto [hello, world] = std::make_tuple("Hello ", "world!"); std::cout << hello << world << std::endl; return 0; } As others have mentioned, from C++17 onwards you can make use of structured bindings for multiple variable assignments.
Combining this with std::array and template argument deduction we can write a function that assigns a value to an arbitrary number of variables without repeating the type or value.
#include <iostream> #include <array> template <int N, typename T> auto assign(T value) { std::array<T, N> out; out.fill(value); return out; } int main() { auto [a, b, c] = assign<3>(1); for (const auto& v : {a, b, c}) { std::cout << v << std::endl; } return 0; } Possible approaches:
memset or {0} the array. struct, and memset or have a constructor that would initialize them to zero.Pointers and references have similar syntax.
Some examples for newbies like me:
// correct. int a = 1, b = 2, c = 3; int *pa = &a, *pb = &b, *pc = &c; int &ra = a, &rb = b, &rc = c; a=4; b=5; c=6; std::cout << ra << "," << rb << "," << rc << '\n'; // 4,5,6 (what we want) std::cout << *pa << "," << *pb << "," << *pc << '\n'; // 4,5,6 (what we want) // incorrect. int a = 1, b = 2, c = 3; // int* pa = &a, pb = &b, pc = &c; // error: invalid conversion from 'int*' to 'int' int& ra = a, rb = b, rc = c; a=4; b=5; c=6; std::cout << ra << "," << rb << "," << rc << '\n'; // 4,2,3 (not 4,5,6) For 0 as initial values, one line is enough. Otherwise, two more lines are needed.
#include <array> #include <iostream> int main(){ std::cout << "Initial values are zeros." << std::endl; auto [a, b, c, d, e, f, g, h] = std::array<int, 8>{}; std::cout << a << b << c << d << e << f << g << h << std::endl; std::cout << "Initial values are non-zeros." << std::endl; std::array<int, 8> arr; arr.fill(1); auto [i, j, k, l, m, n, o, p] = arr; std::cout << i << j << k << l << m << n << o << p << std::endl; } Output:
Initial values are zeros. 00000000 Initial values are non-zeros. 11111111 When you declare a variable without initializing it, a random number from memory is selected and the variable is initialized to that value.
unsigned char and you're using it to assign or initialize another unsigned char (en.cppreference.com/w/cpp/language/default_initialization). Then it's just indeterminate, see Where do the values of uninitialized variables come from, in practice on real CPUs?
int* a, b, c;doesn't do what it looks like).=0for each one in their definitions. And, if you really want many variables, then try an array:int a[10]={0}will initialize eacha[i]to 0 for you.