How to initialize a 2D array correctly? :confused:
-
Hello, How do I initialize a global 2D array in C++? First, I declared the 2D array outside of the main function. int data[32][5]; Then in the main function, I initialized the array: data[32][5] = {{0,0,0,0,0}, {0,0,0,0,1},{0,0,0,1,0},{0,0,0,1,1},{0,0,1,0,0},{0,0,1,0,1}, {0,0,1,1,0}, ......................(rest of the integers here).......... .................................................................................. {1,1,1,1,1}}; The above is what I typed into visual c++ 2005. After the comma of the first bracket and every 5th bracket, I pressed 'Enter' to go to a new line. However, when I compiled, there were syntax errors saying that there are missing ; before '{' or '}' . What is the syntax error? :)
-
Hello, How do I initialize a global 2D array in C++? First, I declared the 2D array outside of the main function. int data[32][5]; Then in the main function, I initialized the array: data[32][5] = {{0,0,0,0,0}, {0,0,0,0,1},{0,0,0,1,0},{0,0,0,1,1},{0,0,1,0,0},{0,0,1,0,1}, {0,0,1,1,0}, ......................(rest of the integers here).......... .................................................................................. {1,1,1,1,1}}; The above is what I typed into visual c++ 2005. After the comma of the first bracket and every 5th bracket, I pressed 'Enter' to go to a new line. However, when I compiled, there were syntax errors saying that there are missing ; before '{' or '}' . What is the syntax error? :)
Put a backslash at the end of each line. This will have the compiler ignore the backslash and the newline character treating the next line as part of the previous line.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
-
Hello, How do I initialize a global 2D array in C++? First, I declared the 2D array outside of the main function. int data[32][5]; Then in the main function, I initialized the array: data[32][5] = {{0,0,0,0,0}, {0,0,0,0,1},{0,0,0,1,0},{0,0,0,1,1},{0,0,1,0,0},{0,0,1,0,1}, {0,0,1,1,0}, ......................(rest of the integers here).......... .................................................................................. {1,1,1,1,1}}; The above is what I typed into visual c++ 2005. After the comma of the first bracket and every 5th bracket, I pressed 'Enter' to go to a new line. However, when I compiled, there were syntax errors saying that there are missing ; before '{' or '}' . What is the syntax error? :)
Can you not do something like:
int data[32][5] = {{0,0,0,0,0},
{0,0,0,0,1},
{0,0,0,1,0},
{0,0,0,1,1},
...
{0,0,1,0,0},
{0,0,1,0,1},
{0,0,1,1,0},
{1,1,1,1,1}};void main( void )
{
...
}
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Hello, How do I initialize a global 2D array in C++? First, I declared the 2D array outside of the main function. int data[32][5]; Then in the main function, I initialized the array: data[32][5] = {{0,0,0,0,0}, {0,0,0,0,1},{0,0,0,1,0},{0,0,0,1,1},{0,0,1,0,0},{0,0,1,0,1}, {0,0,1,1,0}, ......................(rest of the integers here).......... .................................................................................. {1,1,1,1,1}}; The above is what I typed into visual c++ 2005. After the comma of the first bracket and every 5th bracket, I pressed 'Enter' to go to a new line. However, when I compiled, there were syntax errors saying that there are missing ; before '{' or '}' . What is the syntax error? :)
You can only initialize an array with
{ }
at the point where it is declared.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?
-
You can only initialize an array with
{ }
at the point where it is declared.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?
My 5 for seeing the obvious where I failed to... :-O
"It's supposed to be hard, otherwise anybody could do it!" - selfquote