根据建议我修改了代码, 但是如何初始化结构中的单个元素??
#include<stdio.h>
typedef struct student
{
int roll_id[10];
int name_id[10];
} student;
int main()
{
student p = { {0} }; // if i want to initialize single element ''FIX HERE, PLs''
student *pptr=&p;
pptr->roll_id[9]={0}; // here is the error pointed
printf (" %d\n", pptr->roll_id[7]);
return 0;
}
最佳答案
{0}
仅作为聚合(数组或 struct
)初始值设定项有效。
int roll_id[10] = {0}; /* OK */
roll_id[0] = 5; /* OK */
int roll_id[10] = 5; /* error */
roll_id[0] = {0}; /* error */
您似乎想要初始化 struct student
类型的 p
。这是通过嵌套的初始化程序完成的。
student p = { {0} }; /* initialize the array inside the struct */
关于c - 数组初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14193371/