Reputation: 5
I will simplify the problem to be more concrete. Compiling my c++ program with g++ and the -pedantic argument like this:
g++ -Wall -pedantic main.cpp
Gives me the following warning:
warning: ISO C++ does not allow C99 designated initializers [-Wpedantic]
This is what I am trying to implement:
typedef enum {
ARG_HELP_LONG,
ARG_HELP_SHORT,
} arg_t;
static const char *valid_args[] = {[ARG_HELP_LONG] = "--help",
[ARG_HELP_SHORT] = "-h",
}
So that I can compare it to something else:
if (strcmp(argv[i], valid_args[ARG_HELP_LONG]) == 0 || strcmp(argv[i], valid_args[ARG_HELP_SHORT]) == 0) {
do something..
}
What should I use instead?
Also I get a warning telling me that I am not using valid_args, but I do use it. Is there any way of fixing this? Is it related to the above warning?
Upvotes: 0
Views: 1259
Reputation: 122133
Just do not use the designated initialization of the array:
static const char *valid_args[] = {"--help","-h"};
Indices of the two elements are 0
and 1
, respectively. The same as with the designated initializers, because the value of ARG_HELP_LONG
and ARG_HELP_SHORT
are 0
and 1
, respectively.
C++ has designated initializers since C++20, though (from cppreference):
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++.
Upvotes: 2