Jump to content
  • Advertisement
Sign in to follow this  
Dakar

Short C++ question

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

In the gametutorials, the
{0}

code stats to frequently appear. However, nobody bothers to explain what it does. What exactly does it do?

Share this post


Link to post
Share on other sites
Advertisement
Would you mind giving an example of how it is used in the code? And possibly a link to the tutorial where it is used?

Share this post


Link to post
Share on other sites
Qucik sample of the code:

int num_array = {0};



Its for a bubble sort tutorial.

http://www.gametutorials.com/Tutorials/c++/Cpp_Pg6.htm

Share this post


Link to post
Share on other sites
It could be for initializing an integer array of length 1.
Something like :

int dummy[1] = {0};

// same as...
int dummy[1];
dummy[0] = 0;



Otherwise, it should not compile just like this.
I guess 0 would be evaluated as 'false', but it needs a semi-colon ';' right after it.

I hope it all makes sence...

Share this post


Link to post
Share on other sites
It creates an array with 'i' numbers, and it initialises the array with the data that is in the curly brackets ..

so ... you can do the following ....

int num_array[3] = {1,4,7};


and it will put those numbers into their place in the array.

Does that make sense?

Share this post


Link to post
Share on other sites
One uses the curly brace notation to initialise arrays. There needs to be atleast one element specified within the braces, the remaining array elements of type T to be initialised are initialised with the value T()...

int array[3] = {0, 2}; // elements are 0, 2, 0
int array[3] = {0}; // elements are 0, 0, 0

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!