Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

c

Possible Duplicate:
length of array in function argument

My array size is 5. For example:

arrCustId[5]

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

like image 615
neha dhage Avatar asked Apr 09 '11 11:04

neha dhage


1 Answers

If the array is statically allocated, use sizeof(array) / sizeof(array[0])

If it's dynamically allocated, though, unfortunately you're out of luck as this trick will always return sizeof(pointer_type)/sizeof(array[0]) (which will be 4 on a 32 bit system with char*s) You could either a) keep a #define (or const) constant, or b) keep a variable, however.

like image 100
Ben Stott Avatar answered Oct 21 '22 18:10

Ben Stott