unions ?

I want to make a function that accepts 3 calling parameters but one of which is either be a string or an integer. So in an attempt to realise it I started to play with the following.

But I get a warning on APItest.c(62): error: #137: expression must be a modifiable lvalue.

I just cannot get it to work. From what I can find in the Keil manual it should work. I would really appreciate some guidence.

Note I can only look at the results in the watch window if I declare the struct in main as static - is that only way?

#define STRING 'S'
#define INTEGER 'I'

struct one_line
  {
  char type;
  int sn_id;
  int frm_id;
     union {
            char sp[20];
            int si;
     }tag;
  };

int main(void)
{
static struct one_line api;

         api.frm_id= 0x04;
         api.type = INTEGER;
         api.tag.si = 0x0300;
         api.type = STRING;
         api.tag.sp = "Hello"; \ error on this item
etc

Parents
  • You cannot assign a string literal - such as "Hello" - to an array at runtime.
    You can only do this as a special case when you initialise an array in its definition.

    If you want to assign a string literal - such as "Hello" - at runtime, you will have to use a pointer;
    If you want to use an array, you will have to copy the characters of the string using strcpy or similar.

Reply
  • You cannot assign a string literal - such as "Hello" - to an array at runtime.
    You can only do this as a special case when you initialise an array in its definition.

    If you want to assign a string literal - such as "Hello" - at runtime, you will have to use a pointer;
    If you want to use an array, you will have to copy the characters of the string using strcpy or similar.

Children
More questions in this forum