Home > Net >  How to give values to structure variable if it is in const enum in c
How to give values to structure variable if it is in const enum in c

Time:10-22

#include <stdio.h>

typedef enum 
{
  aa = 11, 
  bb = 13
}
data2;

typedef struct
{
  int cc;
  const data2 dd;
}
data1;

int main() 
{
  // HOW TO GIVE VALUES TO data2.dd?? 

  printf("%d", data2.dd) ;
  return 0;
}

CodePudding user response:

Like any const object it can be set only the initializer.

data1 d = { .dd = aa };

CodePudding user response:

data2 and data1 are not objects only type names same as int or float. It is not very good to use the same names for types and struct members (use your imagination and give them some meaningful names).

To set the value:

data1 data3 = {.dd = bb}};

to access

printf("%d", data3.data2.dd) ;
  • Related