Sign in to follow this  
Dakar

Short C++ question

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
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[i] = {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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this