Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using designated initializer with Structs in C

I wanted to test a very simple code but Visual Studio 2010 isn't compiling it. It's the first time I am using designated initializer with structs but it appears to be 100% correct since it's just a simple copy and paste from the wikipedia.

The code is:

#include <stdio.h>

/* Forward declare a type "point" to be a struct. */
typedef struct point point;
/* Declare the struct with integer members x, y */
struct point {
   int    x;
   int    y;
};

/* Define a variable p of type point, and set members using designated  initializers*/
point p = {.y = 2, .x = 1};

 int main()
{
    point p2 = {.y = 3, .x = 4}; //not even inside main it works
    return 0;
}

Visual Studio marks the dots in .y as red. Does anyone knows what's going on? I searched for other references in the internet and I can't see why my code is wrong. Is VS2010 not supporting a C feature?

like image 292
Michel Feinstein Avatar asked Apr 25 '26 21:04

Michel Feinstein


1 Answers

Visual Studio 2010 c compiler supports only C89. Designated initializers are a C99 feature.

like image 194
this Avatar answered Apr 27 '26 10:04

this



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!