1

In C99, a struct array can be initialized

 static struct {char* tag; char* msg;} help_info[] = {[0]={"tag0","msg0"}, [1]={"tag1", "msg1"}};

This is not valid in C++. What is a possible alternative to replicate this behavior for an iterable object containing defined information?

TVB22
  • 47
  • 3
  • 2
    Does this answer your question? [What is the C++ equivalent to C's designated initializers?](https://stackoverflow.com/questions/10859213/what-is-the-c-equivalent-to-cs-designated-initializers) – Krishna Kanth Yenumula Dec 13 '20 at 06:43

3 Answers3

2

As stated in another answer:

From this link designated-initializer not supported in C++ Designated Initializers:

Note: out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++.

However, I believe this code will meet your need.

static struct { const char* tag; const char* msg; } help_info[] = { {"tag0","msg0"}, {"tag1", "msg1"} };
Jeff Spencer
  • 507
  • 2
  • 11
  • This solution works for my context. I appreciate you taking the time to respond. – TVB22 Dec 13 '20 at 07:37
  • Glad it helped. Please mark this answer as solving the problem. It lets people spend time answering other questions rather than reading through a question that's been answered. It also lets others easily find the answer to the same question. – Jeff Spencer Dec 13 '20 at 07:53
2

C++20 added designated initializers to C++. There are some restrictions compared to C, in a couple of different directions. In your case, the main difference of note is that C++ has tightened up a bit of the type system so you can no longer initialize a char * from a string literal.

In C, you can initialize a char * from a string literal for the sake of backward compatibility, but you still have to treat it as if it were a char const *--that is, if you try to write to the string literal you get undefined behavior (and on a modern machine, you'll typically get something on the order of a seg fault that will kill your program).

C++ now demands that you recognize that limitation by using char const * explicitly. If we change your code to suit:

static struct {char const* tag; char const* msg;} help_info[] = 
     {[0]={"tag0","msg0"}, [1]={"tag1", "msg1"}};

...and compile it as C++, it's perfectly fine.

Do note there are other limitations that don't exist in C (but they don't affect this code). For example, C++ demands that the initializers be in order, so in C you also do this:

static struct {char const* tag; char const* msg;} help_info[] = 
     {[1]={"tag1", "msg1"}, [0]={"tag0","msg0"}};

...so the initializer for [1] precedes the initializer for [0], but in C++ that's forbidden.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thanks for taking the time to respond. This response will probably be the best for my context and this question. – TVB22 Dec 13 '20 at 07:40
0

From this link designated-initializer not supported in C++ :

Note: out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++.

I think, it is not possible.

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26