Jump to content
  • Advertisement
Sign in to follow this  
Sabonis

Simple Question

This topic is 5008 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

Hey everyone, I have a quick question about arrays. Suppose I have the code float f[2]; f[0] = 10; f[1] = 20; for(int i = 0 ; i < sizeof(f); i++) cout << f << endl; return 0; I am guessing I am supposed to only get two lines of output... but im getting eight for some reason.. the first two are right but the rest are garbage values and such.. is there an explanation for this? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Yes, sizeof(f) = ( sizeof(float) * 2) = 4 * 2 = 8
Instead what you need to do is: i < 2 instead of sizeof(f).

Sizeof tells the memory size - not the physical array count.

Share this post


Link to post
Share on other sites
for(int i = 0; i < sizeof(f)/sizeof(f[0]); ++i)
This is only possible with arrays, not with pointers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!