Thank you for all your help and support.
My question is about the syntax sugar { 0 } used when initializing arrays in C.
For example, consider the following code. This is the code that led me to ask this question.
It is a function that outputs a histogram of an image, and I know it is hard to read, but I hope you will bear with me.
int histgram(unsigned char image[HEIGHT][WIDTH][3], int hist[256])
{
for (int i = 0; i < 256; i++)
{
hist[i] = 0;
}
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
hist[image[i][j][1]]++;
}
}
return 0;
}
int main(int argc, char** argv)
{
if (argc != 4)
{
fprintf(stderr, "<input.ppm> <output.csv> <output.ppm>");
exit(1);
}
unsigned char image[HEIGHT][WIDTH][3];
loadImage(image, argv[1]);
int hist[256];
histgram(image, hist);
saveImage(image, argv[3]);
return 0;
}
if you initialize the array once and then use the { 0 } syntax when editing its values, like
int hist[N};
hist = { 0 };
you’ll get no outputs.(I have already confirmed this)It is a wonder that there are no errors…
However, initialization with 0 is possible by declaring
int hist[N] = { 0 };
What exactly is {0}?Why doesn’t a code that assigns {0} after initialization fail?
Why can’t { 0 } be used to set all already declared arrays together to 0? Why not use such a language specification?
I asked this question because my knowledge of the C language is very limited.I would appreciate any comments, advice, scolding, etc.