way to get size of an array using something like sizeof?

Started by
6 comments, last by johnnyBravo 20 years, 4 months ago
say ive got an array eg int abc[10]; is there someway i can use a function on it, to determine the size of the array. eg getsize(abc) would equal 10 thanks,
Advertisement
Did you try sizeof(abc)?
Why not use sizeof and then divide the result by the size of the array datatype (ie int in the example you provided)?

[edited by - jack_1313 on January 16, 2004 3:14:01 AM]
returns 40
Which is the size of the array in bytes. If you want the number of elements then try sizeof(abc)/sizeof(int).
cool thanks
Watch out though -- if you pass arrays to functions as pointers, this trick won''t work inside the functions (it''ll give you 4 bytes for the array size as it will just be a pointer). It''s best to either keep a note of the array size in a variable and pass it around or to use a genuine container class (e.g. std::vector) which will keep an internal note of the size.
yeah ive been warned about that a couple of times now , i hate working around that

This topic is closed to new replies.

Advertisement