0

I'm quite puzzled by why my variable NumberOfArrays changes the second time through the for 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
  • 16,685
  • 2
  • 45
  • 81
olafironfoot
  • 45
  • 1
  • 8
  • 1
    Can we all just appreciate how this is an _actual_ stack overflow on **Stack Overflow**? – Charles Oct 04 '16 at 18:25
  • for ease of readability and understandability: 1) please consistently indent the code. Indent after every opening brace '{'. unindent before every closing brace '}'. – user3629249 Oct 05 '16 at 04:28
  • the compiler will output a number of warnings regarding the posted code. Amongst those warnings. warning: unused parameter 'argc' warning: unused parameter: 'argv'. When parameters are not used in main() you can use the prototype: `int main( void )` for other functions, follow the function signature by a statement similar to: `(void)parmName;` – user3629249 Oct 05 '16 at 04:31
  • this line: `int arrayRack[0];` does not make any room for anything. The posted code does not get, from the user, the number of entries in each array. So getting that value needs to be added to the code. Then declare the arrays as: `int arrayRack[ NumberOfArrays ][ NumberOfArrayEntries ];` Then modify the rest of the code to actually input that number of entries for each array, Then modify the rest of the code to output all the entries in each array. – user3629249 Oct 05 '16 at 04:36

2 Answers2

4

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].

Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81
3

You declared the array too early. Move the declaration to after the call of GetInt(), like this:

printf("Key in the number of arrays you'd like to have\n");
int NumberOfArrays = GetInt();
int arrayRack[NumberOfArrays];

Note: NumberOfArrays is not an ideal name for the variable, because it denotes the number of array elements, not the number of arrays; your code has only one array.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523