为什么功能(的char * []数组)有效的函数定义,但不(CHAR(*数组)的C []? [英] Why is function(char * array[]) a valid function definition but not (char (*array)[] in C?

查看:107
本文介绍了为什么功能(的char * []数组)有效的函数定义,但不(CHAR(*数组)的C []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的认为的,这是因为前者是一个指针数组为char,后者是一个指向字符数组,我们需要正确指定对象的大小被指向为我们的函数定义。在前者;

I think that it is because the former is an array of pointers to char and the latter is a pointer to an array of chars, and we need to properly specify the size of the object being pointed to for our function definition. In the former;

function(char * p_array[])

对象的大小被指向到已经包含(其字符指针),但后者

the size of the object being pointed to is already included (its a pointer to char), but the latter

function(char (*p_array)[])

需要阵列p_array点的大小为p_array定义的一部分?
我在哪里,我一直在思考这个太久,刚刚混淆自己的舞台,有人请让我知道如果我的推论是正确的。

needs the size of the array p_array points to as part of p_array's definition? I'm at the stage where I've been thinking about this for too long and have just confused myself, someone please let me know if my reasoning is correct.

推荐答案

两者都是有效的用C ,但不是C ++ 。你的一般的是正确的:

Both are valid in C but not C++. You would ordinarily be correct:

char *x[]; // array of pointers to char
char (*y)[]; // pointer to array of char

然而,阵列衰变的指针,如果它们显示为函数参数。因此,他们成为:

However, the arrays decay to pointers if they appear as function parameters. So they become:

char **x; // Changes to pointer to array of pointer to char
char (*y)[]; // No decay, since it's NOT an array, it's a pointer to an array

在C中的数组类型,尺寸之一是允许不指定。这必须是最左边的一(哎呦,我最右边的说在第一次)。因此,

In an array type in C, one of the sizes is permitted to be unspecified. This must be the leftmost one (whoops, I said rightmost at first). So,

int valid_array[][5]; // Ok
int invalid_array[5][]; // Wrong

(你可以连他们......但我们很少有这样做的原因......)

(You can chain them... but we seldom have reason to do so...)

int (*convoluted_array[][5])[][10];

有一个陷阱,并美中不足的是,与 [] 在一个数组类型是的不完全型。的可以围绕一个指针传递给一个不完整的类型,但某些操作将无法正常工作,因为他们需要一个完整的类型。例如,这是不行的:

There is a catch, and the catch is that an array type with [] in it is an incomplete type. You can pass around a pointer to an incomplete type but certain operations will not work, as they need a complete type. For example, this will not work:

void func(int (*x)[])
{
    x[2][5] = 900; // Error
}

这是一个错误,因为为了找到的地址x [2] ,编译器需要知道有多大 X [0] X [1] 是。但 X [0] X [1] 具有类型 INT [] - 不完全类型有没有关于它有多大的信息。这将成为你想象的类型未腐烂的版本会是这样,这是更清晰的 INT X [] [] - 显然无效C.如果你想各地传递一个二维数组中的C,你有几种选择:

This is an error because in order to find the address of x[2], the compiler needs to know how big x[0] and x[1] are. But x[0] and x[1] have type int [] -- an incomplete type with no information about how big it is. This becomes clearer if you imagine what the "un-decayed" version of the type would be, which is int x[][] -- obviously invalid C. If you want to pass a two-dimensional array around in C, you have a few options:


  • 传递的尺寸参数的一维数组。

  • Pass a one-dimensional array with a size parameter.

void func(int n, int x[])
{
    x[2*n + 5] = 900;
}


  • 使用指针数组的行。这有点笨拙,如果你有真正的2D数据。

  • Use an array of pointers to rows. This is somewhat clunky if you have genuine 2D data.

    void func(int *x[])
    {
        x[2][5] = 900;
    }
    


  • 使用一个固定大小的。

  • Use a fixed size.

    void func(int x[][5])
    {
        x[2][5] = 900;
    }
    


  • 使用可变长度阵列(C99而已,所以它可能不会与微软的编译器工作)。

  • Use a variable length array (C99 only, so it probably doesn't work with Microsoft compilers).

    // There's some funny syntax if you want 'x' before 'width'
    void func(int n, int x[][n])
    {
        x[2][5] = 900;
    }
    


  • 这是一个常见的​​问题,甚至区域对C退伍军人。许多语言缺乏内在的乱用的真正的,可变大小的支持,多维数组(C ++,Java和Python)的虽然有少数语言确实有它(Common Lisp的,哈斯克尔,Fortran语言)。你会看到很多使用数组的数组或手动计算数组偏移code的。

    This is a frequent problem area even for C veterans. Many languages lack intrinsic "out-of-the-box" support for real, variable size, multidimensional arrays (C++, Java, Python) although a few languages do have it (Common Lisp, Haskell, Fortran). You'll see a lot of code that uses arrays of arrays or that calculates array offsets manually.

    这篇关于为什么功能(的char * []数组)有效的函数定义,但不(CHAR(*数组)的C []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆