传递多维数组到函数C [英] Passing multidimensional array to a function C

查看:162
本文介绍了传递多维数组到函数C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题传递一个多维数组给函数在C.无论是在类型定义和参数应该是什么。

I'm having problems passing a multidimensional array to a function in C. Both in the typedef and what the argument should be.

目前,我的功能DEF是这样的:

Currently, my function def looks like this:

int foo(char *a, char b[][7], int first_dimension_of_array);

我宣布一个这样的数组:

I declare an array like this:

char multi_D_array[20][7];

当我尝试调用的函数是这样的:

When I try to call the function like this:

foo(d, multi_D_array, 20);

我得到的警告,第二个参数的功能是不兼容的指针类型。我试过很多,我已经在网上看到了变化,但仍然无法得到它正确。此外,在使用的时候GDB,我发现只有二维数组的第一个阵列中的反馈传递在我做错了会大大AP preciated。

I get the warning that the second argument to the function is from incompatible pointer type. I've tried many of the variations that I've seen online but still can't get it correct. Also, when using GDB, I find that only the first array of the 2D array is passed in. Feedback on what I'm doing wrong would be greatly appreciated.

推荐答案

大警告:我通常不会处理多维数组这样

Big warning: I wouldn't normally handle multi dimensional arrays this way.

我想这只是为了检查,但这:

I tried this just to check, but this:

#include <stdio.h>

int foo(char b[][7])
{
    printf("%d\n", b[3][4]);
    return 0;
}

int main(int argc, char** argv)
{
    char multi_d_arr[20][7];
    multi_d_arr[3][4] = 42;
    foo(multi_d_arr);
    return 0;
}

编译并运行不受任何问题,请使用的gcc -Wall 。我不知道,说实话,第二个参数怎么也算是一个不兼容的指针类型。工作正常。

Compiles and runs without any issue whatsoever, using gcc -Wall. I'm not sure, honestly, how the second argument can be considered an incompatible pointer type. Works fine.

不过,既然你问了,我将如何处理2-D数组?更好的方法是使用像这样的指针:

However, since you ask, how would I handle 2-D arrays? A better way is to use pointers like so:

char** array2d = malloc((MAX_i+1)*sizeof(char*));
for ( i = 0; i < (MAX_i+1); i++ )
{
    array2d[i] = malloc((MAX_j+1)*sizeof(char));
}

现在,仅仅是明确的,您可以访问的最大元素是 array2d [MAX_i] [MAX_j]

Now, just to be clear, the greatest element you can access is array2d[MAX_i][MAX_j].

完成后不要忘了反转过程:

Don't forget to invert the process when done:

for ( i = 0; i < (MAX_i+1); i++ )
{
    free(array2d[i]);
}
free(array2d);

现在,使用这种方法,下标符号仍然有效,因此 array2d [X] [Y] 还是访问正确的价值。从字面上讲, array2d 是一个指针数组,所以是每个 array2d [I]

Now, using this method, subscript notation remains valid, so array2d[x][y] still accesses the right value. Literally speaking, array2d is an array of pointers, and so is each array2d[i].

为什么这是一个更好的方法?好了,你可以拥有可变大小的子阵,如果你这么喜欢。你所要做的就是改变for循环。这己技巧通常用于字符串数组,特别是在 INT主(INT ARGC,字符** argv的)应用程序的签名。 的argv 是可变长度的字符串数组的数组。使用的strlen()来确定它们的长度。

Why is this a better method? Well, you can have variable size sub-arrays if you so like. All you have to do is change the for-loop. This techique is often used for arrays of strings, particularly in the int main(int argc, char** argv) signature of an app. argv is an array of an array of variable-length strings. Use strlen() to determine their length.

能在你身边通过这个?当然。你刚才收到了主,对于初学者,但你也可以写你的函数的签名像这样:

Can you pass this around? Of course. You just received it in main, for starters, but you could also write your function signature like so:

int foo(char** ar2d, const unsigned int size);

和调用它:

char** somearray;
/* initialisation of the above */

foo(ar2d, thesizeofar2d);

/* when done, free somearray appropriately */

只是要清楚,你包括尺寸参数的方法(如 const的无符号整型大小)是非常好的做法,你应该坚持下去。

Just to be clear, your method of including size parameters (like const unsigned int size) is very good practise and you should stick to it.

这篇关于传递多维数组到函数C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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