Dumb array question

Started by
4 comments, last by zackriggle 20 years, 11 months ago
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 --
Advertisement
Yes. Why didn''t you just compile it and see?
And so, the evil religion thread stole the wonderful "What the Heck?" thread's throne. Truly a sad day for gamedev.
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])

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.
no 4
yes

Everything is better with Metal.

This topic is closed to new replies.

Advertisement