0

这是我的代码:

#include<stdio.h>

struct parent_structure
{
    char pe_1 = 'a'; 
};

struct child_structure
{
    int ce_1 = 1;
    int ce_2 = 2;
    int ce_3 = 3;
    struct sample_structure
    {
        int ss_1 = 1 ;
        char ss_2 = 'a';
    } ce_4 ;
    struct parent_structure ce_5;
};

int main()
{
    struct child_structure c_1;
    printf("size of element : %d",sizeof(c_1));

    return 0;
}

错误:

test.c:5:15: error: expected ':', ',', ';', '}' or '_attribute_' before '=' token
     char pe_1 = 'a';
               ^
test.c:10:14: error: expected ':', ',', ';', '}' or '_attribute_' before '=' token
     int ce_1 = 1;
              ^

问题:

* If i initalize value at the time of defining structure
  it gives the error shown above.

我尝试了这个问题一段时间,如果我像在声明结构时那样初始化结构成员的值,它会显示错误。

请告诉我这有什么问题?

4

1 回答 1

0

您不能为 C 中的结构成员提供默认值。结构是数据类型。

就像你声明变量的方式一样,int a;你声明结构变量,如struct child_structure c_1;.

不要与 混淆structstruct struct_namestruct本身不是数据类型,因此struct_name不是变量,因此你不能像你一样初始化它。

于 2020-04-18T15:46:25.650 回答