int x = 750;
int i = 0;
while (pow(2, i) < x) {
i++;
}
printf("i is currently %d\n", i);
int array[i];
while (i > 0){
i--;
printf("The value of array %d is %d\n", i, array[i]);
}
When I do this, it creates some really crazy values. I've heard of trying to use malloc or memset, but the values didn't change. All I want to do is set all of these to zero so that I can use it as a counter. Thanks!
memsetyou tried look like when the values "didn't change" ? Post that please. My crystal ball tells me you invokedmemset(array, 0, i), where you should have invokedmemset(array, 0, i*sizeof(*array));.memset(array, 1, i*sizeof(*array));and it shows all the values as 16843009memsetsets octets; not entities. Setting each byte to 1 in a sequence of 4-byte integers gives 0x01010101 for eachint, which translated to decimal is 16843009. If you want them all to be initially1you need to do it via a hard-loop.for (int j=0; j<i; array[j++] = 1);or something similar.array[j++]into the counter. Thank you so much, this is tremendously helpful!while ((1 << i++) < x);. The version you have would allowito escape the loop as zero if x were1. That would be bad, as it would result in a useless VLA. I would also encourageunsigned intfor all of this.