I am a complete beginner in C, and I have ran into what I am assuming to be a simple error. I have looked up similar issues online but I cannot find an issue with my code. There is very little and I do not know what the issue is.
Here is the error:
C: Array initialization requires a brace-enclosed initializer list
and this is my complete code
#include <stdio.h>
int main() {
char walk[10][10] = { 0 };
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
walk[row][col] = '.';
printf("%c", walk[row][col]);
}
}
getchar();
return 0;
}
When using
char walk[10][10] = { 0 };
I get the compiler error "C: Array initialization requires a brace-enclosed initializer list".
That is your compiler being terribly anal-retentive.
The statement in question is perfectly legal C. It defines a 10x10 2-D array of char named walk
where every element (of 100) is 0
.
To comply with your compiler whims, use one of
char walk[10][10] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* ..., */ { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
char walk[10][10] = { { 0 }, { 0 } };
char walk[10][10] = { { 0 } };
char walk[10][10] = { 0 }; // initial statement wrongly rejected by compiler
Even better (IMHO) would be to configure your compiler to accept legal code.
godbolt.org accepts your initial code
You compile the code with extra warnings enabled, which is a very good idea.
The compiler insists that you initialize an array of arrays with an initializer with the same structure. In your case you can try it char walk[10][10] = { { 0 } };
.
You might have an even more restrictive setting where the compiler indicates that not enough initializers are present. The complete initializer would be:
char walk[10][10] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
Or a more compact version:
char walk[10][10] = { "", "", "", "", "", "", "", "", "", "" };
But looking at your code, walk
does not need an initializer at all since you set all entries right below the declaration:
#include <stdio.h>
int main() {
char walk[10][10];
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
walk[row][col] = '.';
printf("%c", walk[row][col]);
}
}
getchar();
return 0;
}
PS: as pmg says, your code is legal and would compile as is with the default permissive settings, but using the compiler warnings to avoid silly mistakes trumps the extra constraints. Code that compiles cleanly with high warning levels usually has fewer bugs.
Note also that you can initialize walk
with a single call to memset(walk, '.', sizeof(walk));
and you could output single characters more efficiently with putchar(walk[row][col]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With