c - Warning with struct initialization -
i have struct:
struct changeintitem { char *unit; const char **parser; int *changevalue; uint16_t *change_eeprom_value; int maximum; int minimum; };
i want initialize other variables struct-variable:
struct changeintitem changeintitemtypeboolean = { .unit = "", .minimum = 0, .maximum = 1, .parser = {"off", "on"}};
it working fine warnings:
severity code description project file line warning braces around scalar initializer handsteuerung c:\users\... 11 severity code description project file line warning (near initialization 'changeintitemtypeboolean.parser') handsteuerung c:\users\... 11 severity code description project file line warning initialization incompatible pointer type handsteuerung c:\users\... 11 severity code description project file line warning (near initialization 'changeintitemtypeboolean.parser') handsteuerung c:\users\... 11 severity code description project file line warning excess elements in scalar initializer handsteuerung c:\users\... 11 severity code description project file line warning (near initialization 'changeintitemtypeboolean.parser') handsteuerung c:\users\... 11
in case wrote function sets variables of struct default-values prefer method because shorter.
all mistakes caused '.parser = {"off", "on"}' don't mistake...
you can use compound literals, change init to
struct changeintitem changeintitemtypeboolean = { .unit = "", .minimum = 0, .maximum = 1, .parser = (const char *[]){"off", "on"}};
test
#include <stdio.h> #include <stdint.h> struct changeintitem { char *unit; const char **parser; int *changevalue; uint16_t *change_eeprom_value; int maximum; int minimum; }; int main() { struct changeintitem changeintitemtypeboolean = { .unit = "", .minimum = 0, .maximum = 1, .parser = (const char *[]){"off", "on"}}; printf ("%s - %s\n", changeintitemtypeboolean.parser[0], changeintitemtypeboolean.parser[1]); }
output
off - on
Comments
Post a Comment