初始化时跳过结构成员 [英] skipping a structure member while initializing

查看:81
本文介绍了初始化时跳过结构成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。

让我们说,有一个结构

struct struct10 {

int a1;

int a2;

int a3;

int a4;

} count [2] = {

{10,20,30,40},

{50,60,70,80}

};


我怎样才能跳过一个成员,例如a2在初始化时?

非常感谢!

Hallo.
Let''s say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};

How can I skip a member, e.g. a2 while initializing?
Thanks a lot!

推荐答案

Mik0b0写道:
Mik0b0 wrote:

struct struct10 {

int} ='$ _ $ bb a1;

int a2;

int a3;

int a4;

} count [2] = {

{10,20,30,40},

{50,60,70,80}

};


如何跳过会员,例如a2在初始化时?

非常感谢!
Hallo.
Let''s say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};

How can I skip a member, e.g. a2 while initializing?
Thanks a lot!



当结构具有部分初始化器时,没有

初始化器的成员将设置为0.您可以轻松地自己键入0 ;

效果没有区别。

When a structure has a partial initialiser, the members without an
initialiser get set to 0. You can just as easily type 0 yourself;
there''s no difference in effect.


3月10日凌晨2:15,Harald van d 3K"?; < true ... @ gmail.comwrote:
On Mar 10, 2:15 am, "Harald van D?3k" <true...@gmail.comwrote:

Mik0b0写道:
Mik0b0 wrote:

Hallo。

让我们说,有一个结构

struct struct10 {

int a1;

int a2;

int a3;

int a4;

} count [2] = {

{10,20,30 ,40},

{50,60,70,80}

};
Hallo.
Let''s say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};


如何跳过会员,例如a2在初始化时?

非常感谢!
How can I skip a member, e.g. a2 while initializing?
Thanks a lot!



当结构具有部分初始化程序时,没有

初始化程序的成员将设置为0.您可以轻松地自己键入0 ;

效果没有区别。


When a structure has a partial initialiser, the members without an
initialiser get set to 0. You can just as easily type 0 yourself;
there''s no difference in effect.



好​​的,但如果我在每个结构数组中初始化a2之前我怎么办?

之前?

我的意思是


for(i = 0; i< 2; i ++)

{

count [i] .a2 = (i + 1);

};

之后我想初始化所有阵列的所有其他成员?

谢谢!

OK, but what can I do if I initialized a2 in every array of structures
before?
I mean

for (i=0;i<2;i++)
{
count[i].a2=(i+1);
};
and after that I want to initialize all other members of all arrays?
Thanks!


" Mik0b0" < ne **** @ gmail.comwrites:
"Mik0b0" <ne****@gmail.comwrites:

3月10日凌晨2:15,Harald van D?3k < true ... @ gmail.comwrote:
On Mar 10, 2:15 am, "Harald van D?3k" <true...@gmail.comwrote:

> Mik0b0写道:
>Mik0b0 wrote:

你好。

让我们说,有一个结构

struct struct10 {

int a1;

int a2 ;

int a3;

int a4;

} count [2] = {

{10,20 ,30,40},

{50,60,70,80}

};
Hallo.
Let''s say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};


如何跳过会员,例如a2在初始化时?

非常感谢!
How can I skip a member, e.g. a2 while initializing?
Thanks a lot!


当一个结构有一个部分初始化器时,没有
初始化器的成员被设置为0.你可以很容易地自己输入0;
这里有'没有效果差异。


When a structure has a partial initialiser, the members without an
initialiser get set to 0. You can just as easily type 0 yourself;
there''s no difference in effect.



好​​的,但如果我之前在每个结构数组中初始化a2

,我该怎么办?

我的意思是


for(i = 0; i< 2; i ++)

{

count [i] .a2 = (i + 1);

};

之后我想初始化所有阵列的所有其他成员?


OK, but what can I do if I initialized a2 in every array of structures
before?
I mean

for (i=0;i<2;i++)
{
count[i].a2=(i+1);
};
and after that I want to initialize all other members of all arrays?



您只能在声明对象时初始化*对象。如果你执行一个for循环,将值分配给

数组的元素,那么任何初始化都已经完成了。


C99确实提供了复合文字(我从来没有使用它们,所以我不是

会详细介绍),但是没有办法离开一个

洞在一个。你可以将一个复合文字分配给一个struct对象,

但是通过赋值它你会覆盖整个对象。


你能初始化对象吗?设置已跳过成员为0,然后

*然后*使用for循环为这些成员设置值?


-

Keith汤普森(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

You can only *initialize* an object when you declare it. If you''re
executing a for loop that assigns values to the elements of the
arrays, then any initialization is already finished.

C99 does provide compound literals (I''ve never used them, so I''m not
going to go into any more detail), but there''s no way to leave a
"hole" in one. You can assign a compound literal to a struct object,
but by assigning it you''re overwriting the entire object.

Can you initialize the object, setting the "skipped" members to 0, and
*then* use a for loop to set values for those members?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于初始化时跳过结构成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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