Danijel
Danijel

Reputation: 8610

&(float){0} as a float* argument: what it means?

&(float){0}

I came across that in some C code. It was specified as a float* argument in a function. What does it mean?

Upvotes: 2

Views: 160

Answers (2)

0___________
0___________

Reputation: 67713

It is used to pass the constant expression to the function which expects the reference.

Example

float sq(float *f)
{
    *f = *f * *f;
    return *f;
}

int main(void)
{
    printf("%f\n", sq(&(float){3.0f}));
}

Upvotes: 0

HolyBlackCat
HolyBlackCat

Reputation: 96569

It's a compound literal.

foo(&(float){0})

is a shorthand for

float x = 0;
foo(&x);

The only difference between those is that with a compound literal it's impossible to access the number after the call (which matters if foo changes it).

Upvotes: 4

Related Questions