Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Struct initialization with char array outside a function

I have following typedef struct:

typedef struct
{
  E_Menus Menu;        // E_Menus is a typedef enum
  char *MenuStr[200];
  int CurrentItem;
  int ItemAmount;
  int ItemGap;
  int StartItemGap;
} T_Menu;

I want to initialize a struct of type T_Menu as a module variable (not local inside a function) as following:

char mBootMenuStr[] = {"some text"};
T_Menu mBootMenu = {eUI_BootMenu, mBootMenuStr, 1, 2, 1, 2};

But MenuStr makes me struggle. I get 2 warnings:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1    initialization of 'char *' from 'int' makes pointer from integer without a cast [-Wint-conversion] 
2    missing braces around initializer [-Wmissing-braces]

MenuStr should be a string with size 200 -> Means, the string has 200 chars.

When I remove MenuStr it builds without warnings.
What did I miss? Is it impossible to do this outside a function?

Reproducable example:

typedef enum
{
  eUI_BootMenu,
  eUI_LoginMenu,
  eUI_HomeMenu,
} E_Menus;

typedef struct
{
  E_Menus Menu;
  char *MenuStr[200];
  int CurrentItem;
  int ItemAmount;
  int ItemGap;
  int StartItemGap;
} T_Menu;

char mBootMenuStr[] = {"some text"};
T_Menu mBootMenu = {eUI_BootMenu, mBootMenuStr, 1, 2, 1, 2};

Thanks for help in advance.

>Solution :

char *MenuStr[200]; is an array of 200 pointer-to-char.

The initializer in

T_Menu mBootMenu = {eUI_BootMenu, mBootMenuStr, 1, 2, 1, 2};

is treating mBootMenuStr and each expression after it as elements to initialize that array. The integer values of 1 and 2 are being used to initialize elements of type char *, hence the warning.

GCC gives a more detailed diagnostic:

ex.c:21:49: warning: initialization of ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   21 | T_Menu mBootMenu = {eUI_BootMenu, mBootMenuStr, 1, 2, 1, 2};
      |                                                 ^
ex.c:21:49: note: (near initialization for ‘mBootMenu.MenuStr[1]’)
ex.c:21:52: warning: initialization of ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   21 | T_Menu mBootMenu = {eUI_BootMenu, mBootMenuStr, 1, 2, 1, 2};
      |                                                    ^
ex.c:21:52: note: (near initialization for ‘mBootMenu.MenuStr[2]’)
ex.c:21:55: warning: initialization of ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   21 | T_Menu mBootMenu = {eUI_BootMenu, mBootMenuStr, 1, 2, 1, 2};
      |                                                       ^
ex.c:21:55: note: (near initialization for ‘mBootMenu.MenuStr[3]’)
ex.c:21:58: warning: initialization of ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   21 | T_Menu mBootMenu = {eUI_BootMenu, mBootMenuStr, 1, 2, 1, 2};

This warning can be resolved by placing braces around mBootMenuStr, making it the sole initializing element for the MenuStr member of the structure:

T_Menu mBootMenu = {eUI_BootMenu, {mBootMenuStr}, 1, 2, 1, 2};

But it seems likely you simply wanted a string member, not an array of pointers, in the structure:

typedef struct
{
  E_Menus Menu;
  char MenuStr[200];
  int CurrentItem;
  int ItemAmount;
  int ItemGap;
  int StartItemGap;
} T_Menu;

T_Menu mBootMenu = {eUI_BootMenu, "some text", 1, 2, 1, 2};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading