yve*_*owe 2 c++ memory string pointers
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]'
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么我们不能得到地址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
Run Code Online (Sandbox Code Playgroud)
const由于类型不兼容,这对指针也不起作用.
char* const* ptr_const_str_arr = &str_arr; // Does not work either
Run Code Online (Sandbox Code Playgroud)