How do I find the length/number of items present for an array?


Possible Duplicate:
length of array in function argument

My array size is 5. For example:


How can I know how many Customer IDs are already present in my array? In short how to find length of array?

5/23/2017 12:34:21 PM

Do you mean how long is the array itself, or how many customerids are in it?

Because the answer to the first question is easy: 5 (or if you don't want to hard-code it, Ben Stott's answer).

But the answer to the other question cannot be automatically determined. Presumably you have allocated an array of length 5, but will initially have 0 customer IDs in there, and will put them in one at a time, and your question is, "how many customer IDs have I put into the array?"

C can't tell you this. You will need to keep a separate variable, int numCustIds (for example). Every time you put a customer ID into the array, increment that variable. Then you can tell how many you have put in.


I'm not sure that i know exactly what you mean.

But to get the length of an initialized array,

doesn't strlen(string) work ??


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]