We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
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.
"Note I can only look at the results in the watch window if I declare the struct in main as static"
You have defined it as a local variable - so it doesn't actually exist outside the function. If you make it static, it continues to exist but is not visible outside the function by 'C' scoping rules (the debugger has syntax to get around this).
Thanks Andy,
I was in a bookshop last night and definately saw an example written in the way I used it. But I purchased another book Sam's which doesn't. I'm not doubting you, because it doesn't work, but I will go back and take another look at the book for the heck of it.
Thanks about the local, got it.