Short C++ question

Started by
5 comments, last by Dakar 19 years, 4 months ago
In the gametutorials, the

{0}

code stats to frequently appear. However, nobody bothers to explain what it does. What exactly does it do?
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?
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Qucik sample of the code:
int num_array = {0};


Its for a bubble sort tutorial.

http://www.gametutorials.com/Tutorials/c++/Cpp_Pg6.htm
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...
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?
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
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, 0int array[3] = {0}; // elements are 0, 0, 0

Okay, it makes sense now. Thanks!

This topic is closed to new replies.

Advertisement