Archived

This topic is now archived and is closed to further replies.

zackriggle

Dumb array question

Recommended Posts

Can I use sizeof() on an array (not a pointer!) Ex. char myArray[1024]; int x = sizeof(myArray); Does x = 1024? All I need is a yes or no, it was just a thought that came up. ================== My (soon-to-be) Cherished Cookie of Appreciation: -- MattB - for WinSock advice --

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If the type is "int" your sizeof() will be 4096 (on any platform where your code is likely to run; even Merced et al if you use an lp64 compiler).

To get the number of elements in an array, do: sizeof(array)/sizeof(array[0])

Share this post


Link to post
Share on other sites
However, never use sizeof on a dynamically allocated array. sizeof is determined at compile time, so this:


char* letters
letters = (char*)malloc(64);

printf("%d", sizeof(letters));


would output 1.

Share this post


Link to post
Share on other sites