For example, consider the following code. It is a function that outputs a histogram of an image.
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). 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?
3