我可以安全地将&char []转换为char**吗?

Thi*_* D. 1 c arrays pointers char

拥有以下代码:

char data[2048];
Run Code Online (Sandbox Code Playgroud)

并且函数声明如下:

int f(char** data);
Run Code Online (Sandbox Code Playgroud)

我可以安全地这样称呼它:

f((char**)&data);
Run Code Online (Sandbox Code Playgroud)

如果我只使用&data,编译器会发出以下警告:

warning C4047: 'function' : 'char **' differs in levels of indirection from 'char (*)[2048]'
Run Code Online (Sandbox Code Playgroud)

nne*_*neo 7

你不能.

data是一个数组.&data是一个指向数组指针.它不是指向指针的指针.尽管在多个上下文中data 衰减指针,但它本身并不是指针 - 获取地址会为您提供数组的地址.

如果你想要一个指向数组指针的指针,你可能会尝试这样的事情:

char *pdata = data; // data decays to a pointer here
                    // (a pointer to the first element of the array)
f(&pdata);          // Now &pdata is of type char ** (pointer to a pointer).
Run Code Online (Sandbox Code Playgroud)

但是,当然,你真正需要的将取决于你的用例.