Jump to content
  • Advertisement

Archived

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

zackriggle

Dumb array question

This topic is 5621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!