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

The Solution to How do I find the length/number of items present for an array? is


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.

~ Answered on 2011-04-09 11:50:04


Most Viewed Questions: