简体   繁体   English

初始化结构数组时,逗号表达式的左操作数无效

[英]Left-hand operand of comma expression has no effect when initialzing array of a struct

This is the pixel_t struct 这是pixel_t结构

typedef struct pixelType {
   unsigned char r;          /* red field                  */
   unsigned char g;          /* green field                */
   unsigned char b;          /* blue field                 */
} pixel_t;

I am trying to initialize a list of pixel_t structs but am getting an error saying Left-hand operand of comma expression has no effect. 我正在尝试初始化pixel_t结构的列表,但收到一条错误消息,指出逗号表达式的左手操作数无效。 Here is where I initialize it: 这是我初始化它的地方:

pixel_t pixVals[8] = { (pixel_t)(255,255,255), (pixel_t)(255,0,0), (pixel_t)(0,0,255), (pixel_t)(128,0,255), (pixel_t)(0,0,0), (pixel_t)(0,255,0), (pixel_t)(255,128,0), (pixel_t)(255,255,0) };

Any help? 有什么帮助吗?

The proper syntax is: 正确的语法是:

pixel_t pixVals[8] = { {255, 255, 255}, {255, 0, 0}, 

etc. Alternatively, at block scope you can use struct literals: (pixel_t){255, 255, 255} . 或者,在块范围内,您可以使用结构文字: (pixel_t){255, 255, 255}

When you use parentheses you have an expression using the comma operator, which means to discard all but the rightmost operand. 当使用括号时,您将使用逗号运算符来表示表达式,这意味着将丢弃除最右边的操作数之外的所有操作数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM