I have a bluez header file get_opt.h
where argv
is an argument:
extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,.....
which requires char* const*
for argv
. Because the unit is called from a different file I was trying to emulate argv
but whatever permutation of declaring char
, const
and *
is used I get unqualified-id or the compiler moans about not converting to char* const*
. I can get const char *
easily, of course. char const * argv = "0";
compiles OK in Netbeans with C++14 but
char * const * argv = "0";
produces
"error: cannot convert 'const char *' to 'char * const *'" in initialisation (sic).
How do you declare char* const*
or am I mixing C with C++ so I'm up the wrong tree?
Let's review the pointer declarations:
char * -- mutable pointer to mutable data.
char const * -- mutable pointer to constant data.
char * const -- constant pointer to mutable data.
char const * const -- constant pointer to constant data.
Read pointer declarations from right to left.
Looking at the descriptions, which kind of pointer do you want?
Because void main(void) is in a different file I was trying to emulate argv
void main(void)
is not the correct main function declaration. The main
function has to have int
return value.
Simply change the void main(void)
to int main(int argc, char **argv)
in that "other" file.
It does not require char const *
to be passed to. Do not change the type of the main
function arguments.
int getopt_long (int ___argc, char * const *___argv);
int main(int argc, char **argv)
{
printf("%d\n", getopt_long(argc, argv));
}
Both language compile fine: https://godbolt.org/z/5To5T931j
Credit @JaMiT above
[I don't know how to accept a commment]
Also, you might want to notice how you can remove the call to
getopt_long()
from that example while retaining the error (it's motivation for your declaration, but not the source of your issue). If you think about your presentation enough, you might realize that your question is not how to declare achar* const*
but how to initialize it. Due to arrays decaying to a pointer, you could declare an array of pointers (char* const argv[]
) instead of a pointer to pointer (char* const * argv
). See The 1D array of pointers way portion of A: 2D Pointer initialization for how to initialize such a beast. Beware: given your setup, the "2D array way" from that answer is not an option. Reminder: The last pointer in the argument vector is null;argv[argc] == nullptr
when those values are provided by the system tomain()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.