Вопрос

I've written the following line in C. I want to know if is supported by the language. it goes like that:

char * mode[7] = Config_Msg.DHCP ? "DHCP" : "Static";

Basically I want to insert into mode the String value of "DHCP" or "STATIC", depended on the value in Config_Msg.DHCP.

When I compile in IAR, I get this warning:

Warning[Pe520]: initialization with "{...}" expected for aggregate  

What does this warning mean ?

Это было полезно?

Решение

The warning means that the answer to your question is no. What you are doing can be done at runtime, but it is not valid in an initializer. Just write:

char * mode;

mode = Config_Msg.DHCP ? "DHCP" : "Static";

Upon closer inspection, that is not the source of your warning. You had incorrectly declared mode to be an array of 7 pointers, so the compiler was expecting an initialization of the form:

char * mode[ 7 ] = { "one", "two", "three" };

(the remaining four entries will be initialized to all zeros).

Другие советы

You can't assign strings like that, so you should do it like this:

char mode[7];
strcpy(mode,Config_Msg.DHCP ? "DHCP" : "Static");

Note that I also corrected your declaration for mode. You originally declared an array of pointers.

Alternatively, you could also do it with a pointer:

char *mode = Config_Msg.DHCP ? "DHCP" : "Static";

Mystical's answer is correct, but if you want to know the reason your code can't work, it's a bit more complex. You can use a string literal as an initializer for a char array, but like all array objects, string literals in an expression (such as the ?: expression you're using), except as the operand of the & or sizeof operators, decay to pointers. The result of your ?: expression is a pointer, and pointers are not valid initializers for arrays.

For a start, char * mode[7] should be char mode[7] in your example; you're wanting an array of chars not an array of char pointers.

To your question, no you can't do that. You have two choices:

  1. Declare mode as char *mode and make it point to "DHCP" or "Static"
  2. Keep mode as an array of char and copy "DHCP" or "Static" into it.

Note that with the first way your strings will be read-only (ie. stored in the .rodata segment), unlike the second way where they'll be copied into your array and can be modified. However, I don't think that'd be a problem for this example.

You char *mode[7] variable is an array of pointers to chars.

And what you ask for is not allowed. But you could code

char mode[20];

strncpy(mode, sizeof(mode), Config_Msg.DHCP ? "DHCP" : "Static");

edited addition

Actually, as others pointed out, strncpy is dangerous when the limit has been reached. My example should better be

char mode[20];
memset (mode, 0, sizeof(mode));
strncpy (mode, sizeof(mode)-1, Config_Msg.DHCP ? "DHCP" : "Static");

So the last byte of mode remains a zero byte. If you are absolutely sure that the mode string can only be "DHCP" or "Static" you should document that in a comment, and you can declare mode to be of the minimal length (7, which is the number of letters in Static plus 1) and use just strcpy. But imagine that you've got, in a few months, another mode like "automatic". If you did not comment your code, you'll screw yourself.

Even understanding your own code in a few months can be painful. This is why good comments are important.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top