列表中的命名变量? [英] Named variable in a list?

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

问题描述

好的,我有一个简单的问题,我可能只是盲目而且看不到

(可能是简单的)解决方案。我想声明变量,而

同时将它们添加到列表中。我想做的是

这样的事情:


int a,b,c;

int list [] = {& a,& b,& c};


但我只想宣布''a'','b''和'c''一次(他们在这里宣布2

时间,一个用于实例,一个用于列表中的指针)。

现在我认为它可以做到使用宏,但我不能确定如何,即:


声明(list,int,a);

声明(list,int,b);

声明(list,int,c);


declare_instances();

declare_list_pointers();


您需要某种方式将数据存储在临时存储器中,并带有

宏。这样做有什么标准吗? (或者我没见过的完全不同的方法)。


Thnx

Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it. I want to declare variables while
adding them to a list simultaneously. What I want to do is something
like that:

int a, b, c;
int list[] = {&a, &b, &c};

But I only want to declare ''a'', ''b'' and ''c'' once (they''re declared 2
time here, one for the instance and one for the pointer in the list).
Now I figure it''s possible to do that using a macro, but I just can''t
figure out how, ie:

declare(list, int, a);
declare(list, int, b);
declare(list, int, c);

declare_instances();
declare_list_pointers();

You''d need some kind of way to store data in temporary memory with
macros. Is there something standard for doing that? (or a totally
different approach I haven''t seen).

Thnx

推荐答案

Jo *********** ***@gmail.com 写道:

好​​的,我有一个简单的问题,我可能只是盲目而且看不到

(可能是简单的)解决方案。我想声明变量,而

同时将它们添加到列表中。我想做的是

这样的事情:


int a,b,c;

int list [] = {& a,& b,& c};


但我只想宣布''a'','b''和'c''一次(他们在这里宣布2

时间,一个用于实例,一个用于列表中的指针)。
Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it. I want to declare variables while
adding them to a list simultaneously. What I want to do is something
like that:

int a, b, c;
int list[] = {&a, &b, &c};

But I only want to declare ''a'', ''b'' and ''c'' once (they''re declared 2
time here, one for the instance and one for the pointer in the list).



嗯。我不认为那是对的。这将给你

编译器

关于从int *转换为int的诊断。您应该将此

更改为以下之一。


int * list [] = {& a,& b,& c};

int list [] = {a,b,c};


虽然,第二个,你想在之前初始化a,b和c

你使用这些值来填充数组。


此外,它没有声明a,b和c两次。假设它实际上是
工作,a的声明就行了


int a,b,c;


和其他地方。


另外,list是一个糟糕的数组名称。

Um. I don''t think that''s quite right. This is going to give you
compiler
diagnostics about converting from int * to int. You should change this
to one of the following.

int * list[] = {&a, &b, &c};
int list[] = {a,b,c};

Though, with the second, you want to initialize a, b, and c before
you use the values to fill in the array.

Also, it does not declare a, b, and c twice. Assuming it actually
worked, the declaration of a is on the line

int a,b,c;

and nowhere else.

Also, "list" is a poor name for an array.


现在我认为使用宏可以做到这一点,但我不能

弄清楚如何,即:


声明(list,int,a);

声明(list,int,b);

声明(list,int,c);


declare_instances();

declare_list_pointers();

您需要某种方式将数据存储在临时存储器中,并使用

宏。这样做有什么标准吗? (或者我没见过的完全

不同的方法)。
Now I figure it''s possible to do that using a macro, but I just can''t
figure out how, ie:

declare(list, int, a);
declare(list, int, b);
declare(list, int, c);

declare_instances();
declare_list_pointers();

You''d need some kind of way to store data in temporary memory with
macros. Is there something standard for doing that? (or a totally
different approach I haven''t seen).



你真的不清楚你想要做什么不是通过清理你的原始例子来完成
。你能解释一下

为什么原来不能接受?

袜子

It''s really not clear what it is that you want to do that would not be
done by cleaning up your original example. Can you explain differently
why the original would not be acceptable?
Socks


Jo ************** @ gmail.com 写道:

好​​的,我有一个简单的问题,我可能只是盲目而且没有看到

(可能是简单的)解决方案。我想声明变量,而

同时将它们添加到列表中。我想做的是

这样的事情:


int a,b,c;

int list [] = {& a,& b,& c};
Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it. I want to declare variables while
adding them to a list simultaneously. What I want to do is something
like that:

int a, b, c;
int list[] = {&a, &b, &c};



你不能用一系列的int来指示整数。

要么:

int a,b,c;

int * list [] {& a,& b,& c};


或:

int list [3];

You can''t put pointers to ints in an array of ints.
Either:
int a, b, c ;
int * list[] {&a, &b, &c} ;

or:
int list[3] ;


>

但我只想声明''a'',' 'b''和''c''一次(他们在这里宣布2

时间,一个用于实例,一个用于列表中的指针)。

现在我认为使用宏可以做到这一点,但我只是不能确认如何,即:


声明(list,int,a);

声明(list,int,b);

声明(list,int,c);


declare_instances();

declare_list_pointers();


你需要某种方式将数据存储在临时存储器中/>
宏。这样做有什么标准吗? (或者我没见过的完全不同的方法)。


Thnx
>
But I only want to declare ''a'', ''b'' and ''c'' once (they''re declared 2
time here, one for the instance and one for the pointer in the list).
Now I figure it''s possible to do that using a macro, but I just can''t
figure out how, ie:

declare(list, int, a);
declare(list, int, b);
declare(list, int, c);

declare_instances();
declare_list_pointers();

You''d need some kind of way to store data in temporary memory with
macros. Is there something standard for doing that? (or a totally
different approach I haven''t seen).

Thnx



宏是邪恶的。


看起来你正在尝试做类似的事情:


{

int a;

list [0] =& a;

}


然后当该范围退出时你的指针不再有效,

因此试图使用它是未定义的行为。


有点无关,因为如果我说得对你是对的正在努力,

无论如何你不应该使用它,但是如果你需要在

a宏中创建临时工具,一个常见的伎俩就是:

#define MYMACRO \

做\

{\

}而(0)


do / while(0)引入了一个可以放入内容的范围,并且如果你在使用宏之后放了一个分号,那么

在语法上是正确的。 />

-

Alan Johnson

"Macros are evil."

It looks like you are trying to do something like:

{
int a ;
list[0] = &a ;
}

But then when that scope exits your pointer is no longer valid anyway,
so trying to use it is undefined behavior.

Somewhat unrelated, since if I''m right about what you are trying to do,
you shouldn''t use this anyway, but if you need to create temporaries in
a macro, a common trick is something like:
#define MYMACRO \
do \
{ \
} while(0)

The do/while(0) introduces a scope you can put stuff in, and also is
syntactically correct if you put a semicolon after a use of your macro.

--
Alan Johnson


好的,我的意思是我的意思:


int * list [] = {& a ,&b;& c};

原始帖子中的
(google小组不会给编译器警告duh)。


我知道''a''只被声明一次;所有我想说的是,如果你想要

来添加你需要输入两次的东西(声明并添加到

列表中)。嗯,列出一个糟糕的阵列名称,我不在乎

更少;这只是一个愚蠢的例子。


希望这能澄清事情。


Puppet_Sock写道:
Ok, my mistake I meant:

int * list[] = {&a, &b, &c};

in the original post (google groups don''t give compiler warnings duh).

I know ''a'' does get declared only once; all I''m saying is if you want
to add something you gotta type it twice (declaration and adding to the
list). And uhm, list being a poor name for an array, I couldn''t care
less; this is just a dumb example.

Hope this clarifies the thing.

Puppet_Sock wrote:
Jo ************** @ gmail.com 写道:

好​​的,我有一个简单的问题,我可能只是盲目而且看不到

(可能很简单)解决它。我想声明变量,而

同时将它们添加到列表中。我想做的是

这样的事情:


int a,b,c;

int list [] = {& a,& b,& c};


但我只想宣布''a'','b''和'c''一次(他们在这里宣布2

时间,一个用于实例,一个用于列表中的指针)。
Ok, I have a simple problem and I might just be blind and not see the
(possibly simple) solution to it. I want to declare variables while
adding them to a list simultaneously. What I want to do is something
like that:

int a, b, c;
int list[] = {&a, &b, &c};

But I only want to declare ''a'', ''b'' and ''c'' once (they''re declared 2
time here, one for the instance and one for the pointer in the list).



嗯。我不认为那是对的。这将给你

编译器

关于从int *转换为int的诊断。您应该将此

更改为以下之一。


int * list [] = {& a,& b,& c};

int list [] = {a,b,c};


虽然,第二个,你想在之前初始化a,b和c

你使用这些值来填充数组。


此外,它没有声明a,b和c两次。假设它实际上是
工作,a的声明就行了


int a,b,c;


和其他地方。


另外,list是一个糟糕的数组名称。


Um. I don''t think that''s quite right. This is going to give you
compiler
diagnostics about converting from int * to int. You should change this
to one of the following.

int * list[] = {&a, &b, &c};
int list[] = {a,b,c};

Though, with the second, you want to initialize a, b, and c before
you use the values to fill in the array.

Also, it does not declare a, b, and c twice. Assuming it actually
worked, the declaration of a is on the line

int a,b,c;

and nowhere else.

Also, "list" is a poor name for an array.


现在我认为使用宏可以做到这一点,但我不能

弄清楚如何,即:


声明(list,int,a);

声明(list,int,b);

声明(list,int,c);


declare_instances();

declare_list_pointers();

您需要某种方式将数据存储在临时存储器中,并使用

宏。这样做有什么标准吗? (或者我没见过的完全

不同的方法)。
Now I figure it''s possible to do that using a macro, but I just can''t
figure out how, ie:

declare(list, int, a);
declare(list, int, b);
declare(list, int, c);

declare_instances();
declare_list_pointers();

You''d need some kind of way to store data in temporary memory with
macros. Is there something standard for doing that? (or a totally
different approach I haven''t seen).



你真的不清楚你想做什么不会通过清理原来的例子来完成。你能解释一下

为什么原来不能接受?

袜子


It''s really not clear what it is that you want to do that would not be
done by cleaning up your original example. Can you explain differently
why the original would not be acceptable?
Socks


这篇关于列表中的命名变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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