自动生成变量 [英] Automatically generate variables

查看:92
本文介绍了自动生成变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在寻找一种自动在C中声明变量的方法。

我不确定是否有好方法要做到这一点,但我有点想要这样的东西

...


int i;


for(i = 1; i< 4; i ++){


int variable ....


}


例如,这个循环会声明以下变量全部

类型int:


variable1

变量2

变量3


这样的事情可能吗?还有另一种方式吗?


提前致谢,


-Nate

解决方案

Le 13-02-2007,Nate< nv ****** @sbcglobal.netaécrit*:


这样的事情可能吗?还有另外一种方法吗?



数组?

Marc Boyer


Nate< ; nv ****** @ sbcglobal.netwrote:


我正在寻找一种自动在C中声明变量的方法。

我不确定是否有一个很好的方法可以做到这一点,但我有一些

这样的想法...



不,没有好的办法。你的方式尤其错误。 C

不能那样工作。


int i;


for(i = 1; i< 4; i ++){

int variable ....

}


这样的事情可能吗?还有另外一种方法吗?



是;有一种不好的(恕我直言)方式:


#define DECLARE1(type,var)类型var ## 1

#define DECLARE2(type,var )DECLARE1(type,var); \

类型var ## 2

#define DECLARE3(type,var)DECLARE2(type,var); \

类型var ## 3

int main(void){

DECLARE3(int,foo);

返回0;

}


您可以使用其他一些产生尽可能多的#defines

程序(比如一个shell脚本)并将它们放在一个单独的头文件中。


另外,你可以告诉我们你真正*想做什么,我们

可能会建议一个更好的选择。


-

C. Benson Manica |我*应该*知道我在说什么 - 如果我

cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。


Christopher Benson-Manica写道:


Nate< nv ****** @ sbcglobal.netwrote:


>我正在寻找一种在C中自动声明变量的方法。
我不确定是否有这样做的好方法,但我有一些这样的想法...



不,没有好的办法。你的方式尤其错误。 C

不能那样工作。


> int i;


> for(i = 1; i< 4; i ++){
int variable ....
}


>这样的事情可能吗?还有另外一种方法吗?



是的;有一种不好的(恕我直言)方式:


#define DECLARE1(type,var)类型var ## 1

#define DECLARE2(type,var )DECLARE1(type,var); \

类型var ## 2

#define DECLARE3(type,var)DECLARE2(type,var); \

类型var ## 3



或者[稍微]更易于维护的方式:


#define DECLARE(var,seq)var ## seq


>

int main(void){

DECLARE3(int,foo);

返回0;

}



int main( void)

{

int DECLARE(foo,1);

int DECLARE(foo,2);


DECLARE(foo,1)= 10;

foo2 = 5;


printf(" foo1:%d foo2:%d \ n \\ n',DECLARE(foo,1),foo2);


返回(0);

}


>

您可以使用其他一些
程序(比如一个shell脚本)生成尽可能多的#defines并将它们放入单独的头文件。


另外,你可以告诉我们你真正*想做什么,我们

可能会建议一个更好的选择。 />


Hello,

I am looking for a method to automatically declare variables in C.
I''m not sure if there is a good way to do this, but I had something
like this in mind...

int i;

for(i = 1; i < 4; i++){

int variable....

}

For example, this loop would declare the following variables all of
type int:

variable1
variable2
variable3

Is something like this possible? Is there another way?

Thanks in advance,

-Nate

解决方案

Le 13-02-2007, Nate <nv******@sbcglobal.neta écrit*:

Is something like this possible? Is there another way?

An array ?

Marc Boyer


Nate <nv******@sbcglobal.netwrote:

I am looking for a method to automatically declare variables in C.
I''m not sure if there is a good way to do this, but I had something
like this in mind...

No, theres no "good" way. Your way, in particular, is wrong. C
doesn''t work like that.

int i;

for(i = 1; i < 4; i++){
int variable....
}

Is something like this possible? Is there another way?

Yes; there''s a bad (IMHO) way:

#define DECLARE1(type,var) type var##1
#define DECLARE2(type,var) DECLARE1(type,var); \
type var##2
#define DECLARE3(type,var) DECLARE2(type,var); \
type var##3

int main( void ) {
DECLARE3(int,foo);
return 0;
}

You could generate as many #defines as you please using some other
program (say a shell script) and put them in a separate header file.

Alternatively, you could tell us what you *really* want to do and we
can probably suggest a much better alternative.

--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.


Christopher Benson-Manica wrote:

Nate <nv******@sbcglobal.netwrote:

>I am looking for a method to automatically declare variables in C.
I''m not sure if there is a good way to do this, but I had something
like this in mind...


No, theres no "good" way. Your way, in particular, is wrong. C
doesn''t work like that.

>int i;

>for(i = 1; i < 4; i++){
int variable....
}

>Is something like this possible? Is there another way?


Yes; there''s a bad (IMHO) way:

#define DECLARE1(type,var) type var##1
#define DECLARE2(type,var) DECLARE1(type,var); \
type var##2
#define DECLARE3(type,var) DECLARE2(type,var); \
type var##3

Or the [slightly] more maintainable way:

#define DECLARE(var, seq) var##seq

>
int main( void ) {
DECLARE3(int,foo);
return 0;
}

int main(void)
{
int DECLARE(foo, 1);
int DECLARE(foo, 2);

DECLARE(foo, 1) = 10;
foo2 = 5;

printf("foo1: %d foo2: %d\n", DECLARE(foo, 1), foo2);

return(0);
}

>
You could generate as many #defines as you please using some other
program (say a shell script) and put them in a separate header file.

Alternatively, you could tell us what you *really* want to do and we
can probably suggest a much better alternative.


这篇关于自动生成变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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