1. ## Arrays question

New to C, so I'm kind of confused about how C works when it comes to arrays. If I'm given an array with n number of chars in it. How would I go about finding the size of the array, and then grabbing the last char of the array. I was told that C does not put a "null" at the end of an array so one could go on and on before actually finding a "\0" so I was wondering how I might do so using a for loop.

Thanks.

2. That's right. You can't find the size an array by any conventional means unless you store the size somewhere.
For strings, C functions always put a \0 at the end of the array, so functions such as strlen can find the length of the string (in this case, the length of the array is length + 1).
So if it's a string, then you can use strlen to get the length of the string and the array.

3. I was told that C does not put a "null" at the end of an array
String literals automatically have a null character terminator. So "abc" is actually
Code:
`{'a', 'b', 'c', '\0'}`
If I'm given an array with n number of chars in it. How would I go about finding the size of the array, and then grabbing the last char of the array.
Consequently, it depends on how the array is to be interpreted. If the array is actually a null terminated string, you can loop through it until you find a '\0'. Note however that this is finding the string length, not the size of the array. strlen() is also available for use to avoid writing the loop yourself.

4. but what if I was given an array of binary numbers. Would it still fall under the same concept?
ie.
Code:
`{'1','0','1','0','\0'}`

5. but what if I was given an array of binary numbers. Would it still fall under the same concept?
"Binary numbers" is not a type. What you showed is effectively a string literal, "10100". If you are talking about say, an array of ints instead, then no, there will be no such null terminator.

6. oh sorry. So then who would i find the size/length of an array of ints?

7. So then who would i find the size/length of an array of ints?
As Elysia noted, you would store the size somewhere.

8. I'm kind of confused. How would I store the size somwhere if the size varied everytime a value was passed to me?

9. Originally Posted by tlovaj
I'm kind of confused. How would I store the size somwhere if the size varied everytime a value was passed to me?
Whoever passed the array to you must also pass the size. Your function isn't psychic.

10. You can use a struct, similar to C++'s vector.
The struct stores the array and the size. Or you can just pass the array plus the size, which is pretty common in C to avoid buffer overruns.

11. can you give me example code, its hard for me to imagine it.

12. Code:
```void printarray(int* arr, size_t arrsize)
{
}```

13. Originally Posted by tlovaj
oh sorry. So then who would i find the size/length of an array of ints?
Code:
```int arr[10];
size_t s;
s = sizeof (arr) / sizeof (*arr);
printf("size is: &#37;zu\n", s);```

14. Only works in the function you defined the array in, however, to due to the whole arrays decay into pointers ordeal (and you can't find the size of an array from a pointer).

15. Yes, but once it's passed to a function, it is no longer an array.