如何将变量声明为 char* const*?
How to declare a variable as char* const*?
我有一个 bluez 头文件 get_opt.h
其中 argv
是一个参数:
extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,.....
argv
需要 char* const*
。因为该单元是从不同的文件调用的,所以我试图模拟 argv
,但是无论使用声明 char
、const
和 *
的任何排列,我都会得到 unqualified-id 或编译器抱怨不转换为 char* const*
。当然,我可以轻松获得 const char *
。 char const * argv = "0";
在使用 C++14 的 Netbeans 中编译正常但是
char * const * argv = "0";
产生
"error: cannot convert 'const char *' to 'char * const *'" in initialisation (sic).
你如何声明 char* const*
或者我是否将 C 与 C++ 混合在一起所以我走错了树?
Because void main(void) is in a different file I was trying to emulate
argv
void main(void)
不是正确的主函数声明。 main
函数必须具有 int
return 值。
只需将“其他”文件中的 void main(void)
更改为 int main(int argc, char **argv)
。
不需要传给char const *
。不要更改 main
函数参数的类型。
int getopt_long (int ___argc, char * const *___argv);
int main(int argc, char **argv)
{
printf("%d\n", getopt_long(argc, argv));
}
两种语言都可以编译:
https://godbolt.org/z/5To5T931j
让我们回顾一下指针声明:
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.
从右到左阅读指针声明。
看描述,你想要哪种指针?
感谢@JaMiT 以上
[我不知道如何接受评论]
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 a char* 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 to main()
.
我有一个 bluez 头文件 get_opt.h
其中 argv
是一个参数:
extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,.....
argv
需要 char* const*
。因为该单元是从不同的文件调用的,所以我试图模拟 argv
,但是无论使用声明 char
、const
和 *
的任何排列,我都会得到 unqualified-id 或编译器抱怨不转换为 char* const*
。当然,我可以轻松获得 const char *
。 char const * argv = "0";
在使用 C++14 的 Netbeans 中编译正常但是
char * const * argv = "0";
产生
"error: cannot convert 'const char *' to 'char * const *'" in initialisation (sic).
你如何声明 char* const*
或者我是否将 C 与 C++ 混合在一起所以我走错了树?
Because void main(void) is in a different file I was trying to emulate argv
void main(void)
不是正确的主函数声明。 main
函数必须具有 int
return 值。
只需将“其他”文件中的 void main(void)
更改为 int main(int argc, char **argv)
。
不需要传给char const *
。不要更改 main
函数参数的类型。
int getopt_long (int ___argc, char * const *___argv);
int main(int argc, char **argv)
{
printf("%d\n", getopt_long(argc, argv));
}
两种语言都可以编译: https://godbolt.org/z/5To5T931j
让我们回顾一下指针声明:
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.
从右到左阅读指针声明。
看描述,你想要哪种指针?
感谢@JaMiT 以上
[我不知道如何接受评论]
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()
.