Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struct initializer, typedef with visual studio

I wonder why the following does not work with Visual studio

typedef struct {
    float x, y;
} complexf;

typedef union {
    complexf f;
    long long d;
} rope;

int main(void)
{
    complexf a;
    rope z = {a};
}

The error is at line rope z = {a}, cannot convert from complexf to float. If the first member of the union is not a typedef, then it works. Is this a compiler bug, or a dark edge of C ?

like image 391
David Cournapeau Avatar asked Jun 30 '09 17:06

David Cournapeau


2 Answers

ANSI C standard (aka C89), 3.5.7:

All the expressions in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.

The latter part of this restriction has been dropped in C99, which isn't properly supported by VS.

like image 197
Christoph Avatar answered Oct 18 '22 01:10

Christoph


in VS 6.0 when I compile with /W4 I get

warning C4204: nonstandard extension used : non-constant aggregate initializer

so that makes me think it's not standard C and you are in compiler dependent land.

like image 34
jcopenha Avatar answered Oct 18 '22 03:10

jcopenha