[英]c, passing struct as argument
I need to pass some structure as function argument like this 我需要像这样传递一些结构作为函数参数
void myFunc(unsigned char c);
I will use myFunc(4)
, myFunc(8)
or so. 我将使用
myFunc(4)
, myFunc(8)
左右。
Now the function accepts a structure as argument, so I tried 现在函数接受一个结构作为参数,所以我尝试了
typedef struct {
unsigned char address;
unsigned char command;
unsigned char group;
unsigned char response;
unsigned char flags1;
unsigned char flags2;
}test_t;
void myFunc(test_t test);
myFucn({0,0,0,0,0}); // but this gives me error
How can I pass const struct as argument without to have to instantiate first ? 我怎样才能将const struct作为参数传递而不必先实例化? Just like myFunc(4) as unsigned char.
就像myFunc(4)作为unsigned char一样。
Thanks 谢谢
In C99, you can use a compound literal : 在C99中,您可以使用复合文字 :
myFunc((test_t) { 0, 0, 0, 0, 0 });
Of course, since the struct is being passed by value, it doesn't matter if you consider it to be "const" or not; 当然,由于结构是按值传递的,所以如果你认为它是“const”则没关系; whatever the function does to it won't matter to the outside.
无论函数对它做什么都无关紧要。
In previous versions of C, you cannot do this. 在以前的C版本中,您无法执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.