0

Here's my code:

#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;
}

ERROR:

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;
              ^

PROBLEMS:

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

I was trying this question for a while and if I initialize values to members of structure like at the time of declaration of structure it shows an error.

Please tell me what is wrong with this?

Azeem
  • 11,148
  • 4
  • 27
  • 40
Coder
  • 1
  • 1

1 Answers1

0

You cannot give default values to struct members in C. Structures are data types.

Just like the way you declare variables int a; you declare struct variables like struct child_structure c_1;.

Don't confuse struct with struct struct_name, struct itself is not a data type, therefore struct_name is not a variable, and therefore you cannot initialize it as you do.

aulven
  • 521
  • 3
  • 14