我需要像这样传递一些结构作为函数参数
void myFunc(unsigned char c);
我会用myFunc(4)
, myFunc(8)
or so.
现在该函数接受一个结构作为参数,所以我尝试了
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
如何将 const 结构作为参数传递而不必先实例化?
就像 myFunc(4) 作为 unsigned char 一样。
Thanks
在 C99 中,您可以使用复合文字 http://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html:
myFunc((test_t) { 0, 0, 0, 0, 0 });
当然,由于该结构体是按值传递的,因此您是否认为它是“const”并不重要;无论函数做什么,对于外部来说都无关紧要。
在以前的 C 版本中,您无法执行此操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)