{} in assignment

S

slashman

I recently came across code that looked like this

float i = {1.0};

Can anyone tell me the significances of the curly braces{}.
Thanks in advance
Vivek
 
S

santosh

slashman said:
I recently came across code that looked like this

float i = {1.0};

Can anyone tell me the significances of the curly braces{}.
Thanks in advance
Vivek

It's redundant for this particular initialisation, but if i were to be
an array of float then you'd need the curly braces to initialise one or
more of it's elements. Ex:

float arr[5] = { 1.0, 2.0, 3.0 };

Elements arr[0], arr[1] and arr[2] are initialised with 1.0, 2.0 and 3.0
respectively while the remaining elements are set to zero by the
compiler. This is true only for statically allocated arrays.
 
A

Andrey Tarasevich

slashman said:
I recently came across code that looked like this

float i = {1.0};

Can anyone tell me the significances of the curly braces{}.

There's no "assignment" in your code. This is _initialization_, not
assignment. The initializer is enclosed in '{}'. Initializers enclosed
in '{}' are normally used with aggregates. However, it is perfectly
legal to use '{}'-enclosed initializers with scalar objects, in which
case the '{}' are simply redundant. They don't mean anything, meaning
that your code is the exact equivalent of

float i = 1.0;

The ability to use '{}' in scalar initializers might be useful in
"generic" code (in macros, for example), where you can use the common '=
{ 0 }' initialization idiom with a wider range of types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top