Let me quote something again:
Given an array named YourArray, and it's complete size, divided by it's element size named NumberOfElements,
You can only access from:
YourArray[0]
to
YourArray[NumberOfElements-1].
Accessing YourArray in a position higher than (NumberOfElements-1) results in a error.
You need to know its size to avoid such errors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <cstdio>
int main()
{
int YourArray[] = {0,53,512,2416,312,53};
int ArrayNumOfElements = sizeof(YourArray) / sizeof(*YourArray);
for(int i = 0; i < ArrayNumOfElements; ++i)
{
printf("%d\n",YourArray[i]);
}
return 0;
}
|
This example gets the size of YourArray, and prints all the items in the range 0 - (ArrayNumOfElements-1), who are all valid.
If you were to access YourArray[ArrayNumOfElements] or YourArray[ (Number higher than ArrayNumOfElements)] you'd get a error/crash.
You can add or remove items from YourArray with less efforts this way instead of using constants you have to remember to change if you add/remove items.