There are two things that are wrong.
Assuming you want the number of elements in the array, it is done, using:
size_t len = sizeof(arr) / sizeof(*arr)
sizeof
gives the actual size (number of bytes allocated for arr.
- You should start with
len - 1
and not len
.
NOTE: Array indexing is 0 based not 1 based, so the array elements are indexed from 0 to 2
But you would have tried to access arr[3], which can result in undefined behaviour.
- You wrote
i <= 0
. So, i starts from let's say 2
, is 2 <= 0
? NO!
Hence it will never go inside the loop. The correct condition is i >= 0
int len = sizeof(arr) / sizeof(*arr);
for(int i = len - 1; i >= 0; i--)
Well, I don't know why you are taking reverse order input, but a general convention is to take input using:
size_t len = sizeof(arr)/sizeof(*arr);
for (i = 0; i < len; i++)
{
// take input
}
EDIT:
From other comments it seems that you don't understand the for
loop.
Have a look in this answer
Please comment for any further clarification.