在下面的程序中,第5行给出了一个错误,但是11都没有做同样的事情,即用0初始化字符串吗?我非常知道它会导致编译器错误。我的疑问恰恰是如果第5行给出了错误,那么为什么第11行没有给出?
#include<stdio.h>
int main()
{
char name[20]=0; //This should also run if line 11 runs without an error where string is being initialized by zero.
struct node
{
char name[20];
int no;
};
struct node a={0};
}
如果要初始化复合对象(数组,结构,联合),则需要将值放在花括号内{}
。所以你需要写
char name[20] = { 0 }; /* Or '\0' */
字符串是一种特殊情况,由编译器处理,因此您可以编写例如
char name[20] = "";
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句