So, I know that the last element of a string, aka char array is NULL which has the value of 0. If we define a string containing a word of 5 letters, say Stack, we would do it by doing as the following.
char word[5] = "Stack";
And if I wanted to access the first letter of the array, S, I would look for the index 0 by this: word[0]
, similarly for the last letter, k, I would do the index 4 by using word[4]
. But something here doesn't really sit into my mind: we used the number 5 when declaring our array initially.
So my first question is, does the 5 in the declaration mean the program will use the indexes 0 to 5, saying that the index 5 will include the null character.
Now let's say that I want to define an int array which contains whatever, but for the sake of the question it shall include the odd numbers. I do that by typing this:
int odds[5] = {1, 3, 5, 7, 9};
The same goes here, does the 5 mean we will use indexes 0 through 5 and index 5 will take the NULL value? Also, as my second question, does int arrays also end by the NULL character (yes, it's a silly question since it's an int array but at least, does the fifth index include the value 0) As my third question, to make it more general, is there a general rule which says all arrays will end with the value 0?
For the first question, I looked at the net and some references and couldn't really find any answer that clicked.
For the second one, I tried to print the value of odds[5] and it returned 0. Then when I tried to print odds[6], it returned 0 as well, so I doubted the first answer I got for it could be a random value taken from the next adress from the odds[4] index rather than being given that value at the initalization.
Thanks in advance.