0

I've decided to implement the load function using trie, which I've defined as

    typdef struct trie
    {
        struct trie letters[27];
        bool isWord;
    }trie;

then later, when I try to access the last element, i.e, letters[27] as per

    if (c == 96)
    {
         trav->letters[27] = malloc(sizeof(trie));
         trav = trav->letters[27];
    }

The compiler throws an error: array index 27 is past the end of the array (which contains 27 elements) Now, changing the definition of struct to 28 solves the problem, obviously, but when we define an array as - int array[x], doesn't it contain elements from 0 to x? That's how it works elsewhere I think, or am I doing something wrong? Thanks

2

If an array contains 27 elements and the first element is index 0, then the last element is index 26. The last index is always one less than the number of elements, because indexes start at 0.

2
  • But, I think in codeblocks in windows, you could access from 0 to 27 inclusive. Also, in codechef.com/ide you can do that without any problem. So, is it IDE dependent or is it something else? Thanks
    – Mohit Jha
    Nov 17 '17 at 7:01
  • It's the compiler. From this article: "The clang compiler has the parameter -Warray-bounds that is enabled by default. These warnings can be made errors with -Werror=array-bounds.". In every zero-indexed language, there is no index 27 in a 27 element array. It's a bug waiting to happen. Nov 17 '17 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .