C++:无法使用 'char*[x]' 类型的右值初始化 'char**' 类型的变量
C++: cannot initialize a variable of type 'char**' with a rvalue of type 'char*[x]'
char str_arr[] = "ads";
char *str_ptr = str_arr;
char **ptr_str_ptr = &str_ptr; // OK
char **ptr_str_arr = &str_arr; // compile error: cannot initialize a variable of type 'char**' with a rvalue of type 'char*[4]'
我很困惑为什么我们不能得到str_arr
的地址。什么好主意吗?
可以得到str_arr
的地址。然而,它将是一个数组的地址,而不是指针的地址。从本质上讲,赋值失败是因为类型不兼容。
这里有一个例子说明为什么你不能把它分配给指向char
的指针,因为这是可能的:
char **ptr_str_arr = &str_arr; // imagine this has worked
*ptr_str_arr = new char[10]; // This cannot be done to an array
由于类型不兼容,这甚至对const
指针也不起作用。
char* const* ptr_const_str_arr = &str_arr; // Does not work either
演示。
相关文章:
- 枚举类的 C 样式强制转换到基础类型 char 的引用
- 无法使用类型 'const char *' 的左值初始化类型 'char *' 的成员子对象
- C++:将模板类型 char 转换为 std::string
- 获取"rw_ssid"中成员"长度"的错误请求,该成员属于非类类型"char*"
- 将"std::string {aka std::basic_string}"赋值中的不兼容类型<char>
- 成员引用基类型 'char' 不是 C++ 中的结构或联合
- 错误:请求成员 .. 是非类类型"char"
- 错误:为"运算符 std::string {aka std::__cxx11::basic_string}"指定的返回类型<char>
- 为什么类型 char 返回不同的值?
- 请求成员 'begin' in 'c' 中,它是非类类型 'char [101]' sort(c.begin(), c
- C++ 类型 "char" 的参数与类型 "const char" 的参数不兼容
- 类型 "const char *" 的参数与类型 "char *" 的参数不兼容
- 类型 "const char*" 的参数与类型 "char*" 的参数不兼容。但是为什么?:(
- 创建进程使用路径时出现错误事件:类型 "char *" 的 E0167 参数与类型 "LPWSTR" 的参数不兼容
- 类型 "const char *" 的默认参数与类型 "char *" 的参数不兼容
- 返回类型 char* 的成员函数返回在 while 循环后包含不同字符串的地址
- 接受类型char的输入的问题
- 错误:无法递增类型 'char [6]' 的值
- "char *" 类型的值不能用于初始化类型 "char" 的实体
- 类型 "char *" 的参数与类型 "LPWSTR" 的参数不兼容