olafironfoot olafironfoot - 1 year ago 74
C Question

Code to change an array element changes a different variable

I'm quite puzzled by why my variable

changes the second time through the
loop in my code. Can anyone help me out?

#include <stdio.h>
#include <cs50.h>

int main(int argc, string argv[])

//variable declarations
int NumberOfArrays = 0;
int arrayRack[0];

//Get number of arrays
printf("Key in the number of arrays you'd like to have\n");
NumberOfArrays = GetInt();

//Get number for each element in arrayRack[]
for(int i = 0; i < NumberOfArrays; i++)
printf("give me an int for the %i th array\n", i + 1);
arrayRack[i] = GetInt();
// *** on the second pass, my "NumberOfArrays" gets adjusted to my GetInt number here. Why?

//print out numbers stored in respective arrays
for(int j = 0; j < NumberOfArrays; j++)
printf("{%i}<-- number in %ith array\n", arrayRack[j], j + 1);

return 0;


cxw cxw
Answer Source

Because you declared arrayRack as an empty array ([0]). Try int arrayRack[100]; or some other number, and make sure that NumberOfArrays is less than that number before you use it.

Explanation: (edit note that this may vary by compiler) your variables are most likely stored on the stack in nearby memory addresses. So arrayRack points somewhere close to NumberOfArrays in memory. C doesn't generally check if you've run off the end of an array, so accessing arrayRack[1] doesn't cause a compiler error in this situation. However, arrayRack[1] isn't part of your array, so accessing it actually accesses something else — in this situation, NumberOfArrays.

Edit gcc permits length-0 arrays but does not allocate space for them per this. However, length-0 arrays are prohibited by the C standard (e.g., see this, the answers to this, and this). Given the behaviour you've seen, it looks to me like the compiler is allocating one int's worth of space on the stack, pointing arrayRack to that space, and packing that space right next to NumberOfArrays. As a result, &(arrayRack[1]) == &NumberOfArrays. In any event, using variable-length arrays as suggested by @dasblinkenlight is a cleaner way to handle this situation.

In general, given int arrayRack[N];, you can only safely access arrayRack[0] through arrayRack[N-1].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download