即时创建匿名本地变量以及更多... [英] Creating anonymous local variables on-the-fly and more...

查看:53
本文介绍了即时创建匿名本地变量以及更多...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


给出以下结构


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。

解决方案

< gi ************ @ agamura.comwrote in message

news:11 ******** **************@r29g2000hsg.googlegr oups.com ...


大家好,

给定以下结构



[...]


>

...结束以下函数



[...]


>

是否可以实现类似



[...]


我不知道你为什么要那样做。无论如何,这基本上是我现在所能想到的所有现金:

__________________________


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;

}


__________________________

奇怪......


" Chris Thomasson" < cr ***** @ comcast.netwrote in message

新闻:-O ************************ ****** @ comcast.com ...

[...]


__________________________


[...]


>

#define MY_MACRO(mp_func,mp_sinit){\\ \\ / $
MyStruct Temp = mp_sinit(); \

foo(& Temp); \

}



oops!应该读作:

#define MY_MACRO(mp_func,mp_sinit){\

MyStruct Temp = mp_sinit(); \

mp_func(& Temp); \

}

[...]


__________________________



对于任何混淆感到抱歉。


gi ************ @ agamura.com 写道:


大家好,<给定以下结构


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)); /* 怎么样?那可能吗? * /

}



除了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 should
automatically create
a MyStruct variable onto the stack and return its address; is
that possible?
*/
foo(my_macro(1, 2)); /* How? Is that possible? */
}

Thanks,
j3d.

解决方案

<gi************@agamura.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...

Hi guys,

Given the following structure

[...]

>
... end the following function

[...]

>
is it possible to implement something like

[...]

I have no idea why you want to do that. Anyhow, this is basically all I can
think 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...


"Chris Thomasson" <cr*****@comcast.netwrote in message
news:-O******************************@comcast.com...
[...]

__________________________

[...]

>
#define MY_MACRO(mp_func, mp_sinit) { \
MyStruct Temp = mp_sinit(); \
foo(&Temp); \
}

oops! that should read as:
#define MY_MACRO(mp_func, mp_sinit) { \
MyStruct Temp = mp_sinit(); \
mp_func(&Temp); \
}
[...]

__________________________

Sorry for any confusion.


gi************@agamura.com wrote:

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 should
automatically create
a MyStruct variable onto the stack and return its address; is
that possible?
*/
foo(my_macro(1, 2)); /* How? Is that possible? */
}

Apart from Chris Thomasson''s suggestion, which has its own problems
(name clashes between the temporary variable and other variables; what
if you want to use the results of the call to foo()?; etc.), I can think
of no way to do what you want in C.

Perhaps you''d like to explain what you are trying to achieve and we may
have some other suggestions.


这篇关于即时创建匿名本地变量以及更多...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆