如何在结构中初始化和数组? [英] How to initialize and array in a structure?

查看:63
本文介绍了如何在结构中初始化和数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在以下代码行中设置[0] = 3的值?


#include< stdio.h>

struct letter {

char a [0];

};


struct add {

struct letter addit;

};


int main(void){

struct add test;

返回0;

}

当我尝试类似test.add = 3或test.addit.a = 3时,
,编译器

给我警告

''赋值中的不兼容类型''。


提前致谢

Chad

解决方案




Chad写道:
< blockquote class =post_quotes>如何在以下代码行中设置[0] = 3的值?

#include< stdio.h>
struct letter {
char a [0];
};

struct add {
struct letter addit;
};

int main(void){
struct add test;
re转0;
}
当我尝试像test.add = 3或test.addit.a = 3时,编译器
给了我警告
' 分配中不兼容的类型。

提前致谢
Chad




char a [0]是零长度数组你不能把它设置成任何东西。

假设

char a [1];

试试

test .add [0] = 3;


Chad写道:

如何在下面设置[0] = 3的值代码行?

#include< stdio.h>

结构字母{
char a [0];
C中不允许长度为0的数组.ITYM:

char a [1];

(这不是一个整体很有意义。)};

struct add {
struct letter addit;
};

int main(void){
struct add test;
/ *赋值* /

test.addit.a [0] = 3;返回0;
}
当我尝试像test.add = 3或test.addit.a = 3时,编译器
给了我警告
' '赋值中的不兼容类型''。



当然,你需要意识到赋值和初始化是两个非常不同的东西。


要*初始化*'test''你写的值:


int main(void){

struct add test = {{{3}}};

返回0;

}


HTH,

--g

-

Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com (新帖子8/5)
http://www.cafepress.com/goldsays

如果你没有什么可隐瞒的,你就是不要尝试!"




Artie Gold写道:

Chad写道:

如何在以下代码行中设置[0] = 3的值?

#include< stdio.h>

结构字母{
char a [0];


C中不允许长度为0的数组.ITYM:
char a [1 ];
(这也没有多大意义。)

};

struct add {
struct letter addit;
};

int main(void){
struct add test;


/ * assignment * /
test.addit.a [0] = 3;

返回0;
}
当我尝试类似test.add = 3或test.addit.a = 3时,编译器
给了我警告
''赋值中不兼容的类型''。


当然,你需要意识到赋值和初始化是两个截然不同的东西。

要初始化*'test''你写的值:

int main(v oid){
struct add test = {{{3}}};
返回0;
}

HTH,
--ag

-
Artie Gold - 德克萨斯州奥斯汀
http ://goldsays.blogspot.com (新帖子8/5)
http://www.cafepress.com/goldsays
如果你没有什么可隐瞒的,你就不会尝试!




好​​的,我想我得到了这个。如果没有,我会在某个时候回到这里

明天会提出更多问题。


How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];
};

struct add {
struct letter addit;
};

int main(void) {
struct add test;
return 0;
}

when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning
''incompatible types in assignment''.

Thanks in advance
Chad

解决方案



Chad wrote:

How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];
};

struct add {
struct letter addit;
};

int main(void) {
struct add test;
return 0;
}

when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning
''incompatible types in assignment''.

Thanks in advance
Chad



char a[0] is a zero length array you can not set it to any thing.
assuming
char a[1];
try
test.add[0]=3;


Chad wrote:

How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0]; An array with a length of 0 is not permitted in C. ITYM:
char a[1];
(Which doesn''t make a whole lot of sense either.) };

struct add {
struct letter addit;
};

int main(void) {
struct add test; /* assignment */
test.addit.a[0] = 3; return 0;
}

when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning
''incompatible types in assignment''.


Of course, you need to realize that assignment and initialization are
two very different things.

To *initialize* the value of `test'' you''d write:

int main(void) {
struct add test = {{{3}}};
return 0;
}

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"



Artie Gold wrote:

Chad wrote:

How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];


An array with a length of 0 is not permitted in C. ITYM:
char a[1];
(Which doesn''t make a whole lot of sense either.)

};

struct add {
struct letter addit;
};

int main(void) {
struct add test;


/* assignment */
test.addit.a[0] = 3;

return 0;
}

when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning
''incompatible types in assignment''.


Of course, you need to realize that assignment and initialization are
two very different things.

To *initialize* the value of `test'' you''d write:

int main(void) {
struct add test = {{{3}}};
return 0;
}

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"



Okay, I think I got this. If not, I''ll be back on here sometime
tommorrow asking more questions.


这篇关于如何在结构中初始化和数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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