C语言 C - 'char **' 与 'char (*)[6]' 的间接级别不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7524070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:42:03  来源:igfitidea点击:

C - 'char **' differs in levels of indirection from 'char (*)[6]'

c

提问by loop

Can someone please explain to me what's wrong with the following, and more importantly why?

有人可以向我解释以下内容有什么问题,更重要的是为什么?

int main( int argc, char *argv[] )
{
    char array[] = "array";
    char **test;
    test = &array;

    *test[0] = 'Z';

    printf( "%s\n", array );

    return 0;
}

EDIT

编辑

My example above was based on a function like this that was crashing:

我上面的例子基于这样一个崩溃的函数:

void apple( char **pp )
{
    *pp = malloc( 123 );
    *pp[0] = 'a'; // technically this is correct but in bad form
    *pp[1] = 'b'; // incorrect but no crash
    *pp[2] = '
void apple( char **pp )
{
    *pp = malloc( 123 );
    (*pp)[0] = 'a'; // correct
    (*pp)[1] = 'b'; // correct
    (*pp)[2] = '
int main( int argc, char *argv[] )
{
  char array[] = "array";
  char (*test)[6];
  test = &array;

  (*test)[0] = 'Z';

  printf( "%s\n", array );

  return 0;
}
'; // correct }
'; // incorrect and crash }

As pointed out to me by Vaughn Cato although *pp[0] = 'a';does not crash it is in bad form. The correct form is the parenthesis

正如 Vaughn Cato 向我指出的那样,虽然*pp[0] = 'a';没有崩溃,但它的状态很差。正确的形式是括号

int main( int argc, char *argv[] )
{
  char array[] = "array";
  char *test;
  test = array;  // same as test = &array[0];

  test[0] = 'Z';

  printf( "%s\n", array );

  return 0;
}

Also as another poster MK pointed out the FAQ covers the difference between arrays and pointers: http://www.lysator.liu.se/c/c-faq/c-2.html

同样作为另一位海报 MK 指出常见问题解答涵盖了数组和指针之间的区别:http: //www.lysator.liu.se/c/c-faq/c-2.html

采纳答案by sashang

test = &array

test = &array

is wrong because test is of type char**and &arrayis a char(*)[6]and is a different type from char**

是错误的,因为 test 属于类型char**并且&array是 achar(*)[6]并且是与char**

An array isn't the same type as char*although C will implicitly convert between an array type and a char*in some contexts, but this isn't one of them. Basically the expectation that char*is the same as the type of an array (e.g: char[6]) is wrong and therefore the expectation that taking the address of an array will result in a char**is also wrong.

数组与char*尽管 C 会char*在某些上下文中隐式地在数组类型和 a 之间进行转换的类型不同,但这不是其中之一。基本上与char*数组类型相同的期望(例如:)char[6]是错误的,因此期望获取数组的地址将导致 achar**也是错误的。

回答by Vaughn Cato

This would be the way to do what you seem to be trying to do:

这将是做你似乎想要做的事情的方法:

##代码##

test is a pointer to an array, and an array is different from a pointer, even though C makes it easy to use one like the other in my cases.

test 是一个指向数组的指针,而数组与指针不同,即使在我的情况下,C 可以很容易地使用一个数组。

If you wanted to avoid having to specify a specific sized array, you could use a different approach:

如果您想避免指定特定大小的数组,您可以使用不同的方法:

##代码##

回答by cpx

char **test;is a pointer to a pointer, but if you're going to take the address of an entire array then it needs to be a pointer to entire array i.e char (*test)[6];

char **test;是指向指针的指针,但如果您要获取整个数组的地址,则它需要是指向整个数组的指针,即 char (*test)[6];