简体   繁体   中英

warning during initialization of struct '… makes pointer from integer without a cast'

I have the following situation:

struct Lamp {
  char *highAddress [9];
  char *lowAddress [9];
  bool OnOff;
  bool active;
  uint8_t power;
  uint8_t brightness;
  uint8_t temperatures;
  uint8_t faultCount;
} ;

struct lamps Lamp [] = {
  { (char *) "0013A200" , (char *) "4094500D" , false, true , 0, 0 , 0, 0 } ,
  { (char *) "0013A200" , (char *) "40B12530" , false, true , 0, 0 , 0, 0 } ,
  { (char *) "11111111" , (char *) "22222222", false , false, 0, 0 , 0, 0 } ,
  { (char *) "33333333" , (char *) "44444444", false , false, 0, 0 , 0, 0 } ,
  { (char *) "55555555" , (char *) "66666666", false , false, 0, 0 , 0, 0 } ,
  { (char *) "77777777" , (char *) "88888888", false , false, 0, 0 , 0, 0 } ,
  { (char *) "99999999" , (char *) "00000000", false , false, 0, 0 , 0, 0 } ,
  { (char *) "AAAAAAAA" , (char *) "BBBBBBBB", false , false, 0, 0 , 0, 0 } ,
  { (char *) "CCCCCCCC" , (char *) "DDDDDDDD", false , false, 0, 0 , 0, 0 } ,
  { (char *) "EEEEEEEE" , (char *) "FFFFFFFF", false , false, 0, 0 , 0, 0 } ,
 };

compiling with avr- gcc- C99 I get the message shown.

What might be?

Thank you very much.

Domenico

The members highAddress and lowAddress are arrays of pointers . Just make them simple pointers (or arrays) and it will work fine:

struct Lamp
{
    char *highAddress;
    char *lowAddress;
    ...
};

Joachim already gave you a valid answer, this here as a complement, it is probably to long for just a comment.

In addition, there would be several minor things to consider with your code:

  • string literals are unmutable, so the types for those two fields would better be char const* . Otherwise, if your code has to change them later, do the opposite of what Joachims says, in a way, and use char highAddress[9] to have a copy of the string in your struct .

  • In most contexts an string literal "abcde" "decays" into a pointer to its base type, so char* . In that sense the casts that you are doing wouldn't help you anything. Don't use casts unless you really must and you really know what you are doing. Here, in particular, they are really counter productive since in an initializer they may have a different role when initializing a char array. For the variant char highAddress[9] the cast would be plain wrong.

  • There is a construct in C99 called designated initializers that would have helped you know a bit better what was going on:


struct lamps Lamp [] = {
  { .highAdress = "0013A200" ,
    .lowAdress = "4094500D" ,
    .OnOff =  false, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM