mohammadsdtmnd mohammadsdtmnd - 10 months ago 111
C Question

Initialize enum by string in c (keil v5)uvision

I'm using these code to receive string from uart then matching them with this enum then putting them on switch-case.

char temp[3],rx_buf[100];
enum form {
GGA = 1,
enum form gnssform ;


I can't understand that we can directly use something like EXAMPLE:


and there is no error ,but this:


not compiling .and please tell me the possible way to do this???,because of this EXAMPLE I believe that it must be possible.
the error is :

a value of type "char *" cannot be assigned to an entity of type "enum form"

please do not tell me to use if-else because I hate that.

Finally I used

else if(rx_buf[3]=='G'&&rx_buf[4]=='S'&&rx_buf[5]=='A')gnssform=GSA;

Answer Source

GSA is a constant value of enum form.

temp is a char pointer to the three letters 'GSA'.

They are not the same thing, and you cannot directly assign them.