本文介绍了即时创建匿名本地变量以及更多...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 给出以下结构 typedef struct _MyStruct { int a; int b; } MyStruct; ....结束以下函数 void foo(MyStruct * myStruct) { ... } 是否可以实现类似 int main() { / *传统方式* / MyStruct s = {1,2}; foo(& s); / * OK * / / *而不是声明局部变量s,my_macro应该 自动创建 a MyStruct变量到堆栈并返回其地址;这可能是吗? * / foo(my_macro(1,2)); /* 怎么样?那可能吗? * / } 谢谢, j3d。 解决方案 [...] [...] [...] 我不知道你为什么要那样做。无论如何,这基本上是我现在所能想到的所有现金: __________________________ typedef struct _MyStruct { int a; int b; } MyStruct; void foo(MyStruct * myStruct){ } #define MY_MACRO(mp_func,mp_sinit){\ MyStruct Temp = mp_sinit(); \ foo(& Temp); \ } int main(无效){ #define FOO_SINIT(){1,2} MY_MACRO(foo,FOO_SINIT); 返回0; } __________________________ 奇怪...... oops!应该读作: #define MY_MACRO(mp_func,mp_sinit){\ MyStruct Temp = mp_sinit(); \ mp_func(& Temp); \ } [...] 对于任何混淆感到抱歉。 除了Chris Thomasson的建议,它有自己的问题 (临时变量和其他变量之间的名称冲突; 如果你想使用调用foo()的结果?等等,我可以认为 无法在C中做你想做的事。 也许你想解释一下你想要达到的目标,我们可能会有一些b $ b其他建议。 Hi guys, Given the following structure typedef struct _MyStruct{int a;int b;} MyStruct; .... end the following function void foo(MyStruct *myStruct){...} is it possible to implement something like int main(){/* traditional way */MyStruct s = {1, 2};foo(&s); /* OK */ /* instead of declaring the local variable s, my_macro shouldautomatically createa MyStruct variable onto the stack and return its address; isthat possible?*/foo(my_macro(1, 2)); /* How? Is that possible? */} Thanks,j3d. 解决方案 [...][...][...] I have no idea why you want to do that. Anyhow, this is basically all I canthink of for now:__________________________ typedef struct _MyStruct {int a;int b;} MyStruct; void foo(MyStruct *myStruct) { } #define MY_MACRO(mp_func, mp_sinit) { \MyStruct Temp = mp_sinit(); \foo(&Temp); \}int main(void) {#define FOO_SINIT() {1, 2}MY_MACRO(foo, FOO_SINIT);return 0;} __________________________Weird... [...]oops! that should read as:#define MY_MACRO(mp_func, mp_sinit) { \MyStruct Temp = mp_sinit(); \mp_func(&Temp); \}[...]Sorry for any confusion. Apart from Chris Thomasson''s suggestion, which has its own problems(name clashes between the temporary variable and other variables; whatif you want to use the results of the call to foo()?; etc.), I can thinkof no way to do what you want in C. Perhaps you''d like to explain what you are trying to achieve and we mayhave some other suggestions. 这篇关于即时创建匿名本地变量以及更多...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 12:52