lundi 2 mars 2015

Strange values while initializing array using designated initializers



When I initialize the array below all the output looks ok except for values[3]. For some reason values[3] initialized as values[0]+values[5] is outputting a very large number. My guess is that I am trying to assign values[0]+values[5] before they are properly stored in memory but if someone could explain that would be great.



int main (void)
{

int values[10] = {
[0]=197,[2]=-100,[5]=350,
[3]=values[0] + values[5],
[9]= values[5]/10
};

int index;

for (index=0; index<10; index++)
printf("values[%i] = %i\n", index, values[index]);


return 0;
}


The output is as follows:



values[0] = 197
values[1] = 0
values[2] = -100
values[3] = -1217411959
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35



Aucun commentaire:

Enregistrer un commentaire