0

Considering the following:

#define SIZE 5

/* ... */

int oxen[SIZE] = {5,3,2,8};
int yaks[SIZE];
yaks[SIZE] = oxen [SIZE];   /* -->Out of range */

Can someone explain why its out of range?

xbug
  • 1,394
  • 1
  • 11
  • 18
  • 2
    Yes. For an array of size 5, the valid subscripts (indexes) are 0-4, so there is no `yaks[5]` or `oxen[5]`. – Ken White Nov 28 '14 at 00:02
  • 3
    Because you have not read page one of the "arrays" section in any C book or tutorial. – John3136 Nov 28 '14 at 00:02
  • @John3136 or any other page either; I'm guessing that he is trying to assign all elements – M.M Nov 28 '14 at 00:51

3 Answers3

3

Arrays indices in C start at 0, so your oxen and yaks arrays range from 0 to SIZE-1. You're outside the allowed boundaries, as the compiler rightly warns you about.

xbug
  • 1,394
  • 1
  • 11
  • 18
1

Because C is zero-indexed, oxen[SIZE] is really trying to deference a sixth element that does not exist. Use the index [SIZE-1], instead.

Community
  • 1
  • 1
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
0

because c Language can access the elements of array by their indexes thus index start from 0 to n-1 where n is a number of elements in your array so in your case u can access elements from 0 to 4 indexes if u deal with array of characters maybe this works because of null terminator '\0' also u may not get an errors and get random results because its compiler dependent :)