How to Initialize a Point Array?

I am writing some Win32 program. and I meet a problem. I define an array of Point,just like this:

   POINT points[3];

and now I want to Initialize it, and I know this is illegal

   POINT points[3] = { (295,295),(200,200),(400,500) };

so I need the correct way.


You can do it simply as

POINT points[3] = { 295, 295, 200, 200, 400, 500 };

but a safer thing to do would be this

POINT points[3] = { { 295, 295 }, { 200, 200 }, { 400, 500 } };

The amusing part is that what you originally wrote is not illegal (where did you get that idea?). The () you used inside your initializer will cause the inner , to be interpreted as comma operator. For example, expression (400, 500) evaluates to 500 . That means that your original initializer is actually treated as

POINT points[3] = { 295, 200, 500 };

which is in turn equivalent to

POINT points[3] = { { 295, 200 }, { 500, 0 }, { 0, 0 } };

It doesn't do what you want it to do, but it is certainly not illegal.


根据评论:

POINT points[] = {{295,295}, {200,200}, {400,500}};
链接地址: http://www.djcxy.com/p/84430.html

上一篇: C ++切换语句案例错误

下一篇: 如何初始化点数组?