在C中传递数组的数组 [英] Passing an array of arrays in C

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

问题描述

我需要一个具有2D数组并生成随机位的函数,因此结果是一个随机二进制字符串数组.

I need to have a function which takes a 2D array and generates random bits, so the result is an array of random binary strings.

我有以下代码,

#define pop_size 50
#define chrom_length 50
main() {
    int population[pop_size][chrom_length];
    init_pop(&population);
}
int init_pop(int *population[][]) {
    for(i = 0; i < pop_size; i++) {
        for(j = 0; j < chrom_length; j++) {
            *population[i][j] = rand() % 2;
        }
    }
    return 0;
}

在编译时,我收到以下错误消息:

On compilation, I am getting the following error message:

数组类型的元素类型不完整

array type has incomplete element type

有什么建议吗?

推荐答案

通常的时间...

在大多数情况下出现数组表达式时,其类型都会从"T的N个元素数组"隐式转换为指向T的指针",并且其值设置为指向数组的第一个元素.该规则的例外情况是:数组表达式是sizeof或一元&运算符的操作数,或者它是用作声明中的初始化程序的字符串文字.

When an array expression appears in most contexts, its type is implicitly converted from "N-element array of T" to "pointer to T", and its value is set to point to the first element of the array. The exceptions to this rule are when the array expression is an operand of either sizeof or the unary & operators, or if it is a string literal being used as an initializer in a declaration.

这在您的代码上下文中意味着什么?

What does all that mean in the context of your code?

表达式population的类型是"pop_size-chrome_length的元素数组-int的元素数组".按照上述规则,在大多数情况下,表达式population将隐式转换为指向chrome_length -int元素的数组的指针"或int (*)[chrome_length]的类型.

The type of the expression population is "pop_size-element array of chrome_length-element arrays of int". Going by the rule above, in most contexts the expression population will implicitly be converted to type "pointer to chrome_length-element arrays of int", or int (*)[chrome_length].

但是,表达式&population的类型是指向pop_size-chrome_length的元素数组的指针-int的元素数组"或int (*)[pop_length][chrome_size]的指针,因为population是一元的操作数&运算符.

The type of the expression &population, however, is "pointer to pop_size-element array of chrome_length-element arrays of int", or int (*)[pop_length][chrome_size], since population is an operand of the unary & operator.

请注意,这两个表达式具有相同的 value (数组第一个元素的地址),但类型不同.

Note that the two expressions have the same value (the address of the first element of the array), but different types.

根据您编写的代码,将函数称为

Based on the code you've written, where you call the function as

init_pop(&population);

相应的函数定义应为

int init_pop(int (*population)[pop_size][chrome_length]) // note that both dimensions
                                                         // must be specified

,您将以以下方式访问每个元素

and you would access each element as

(*population)[i][j] = initial_value;

请注意,这意味着init_pop只能 处理pop_size x chrome_length数组;您不能在不同大小的数组上使用它.

Note that this means init_pop can only deal with pop_size x chrome_length arrays; you can't use it on arrays of different sizes.

如果您以

init_pop(population); // note no & operator

则相应的函数定义必须为

then the corresponding function definition would have to be

int init_pop(int (*population)[chrome_length]) // or population[][chrome_length],
                                               // which is equivalent

,您将以以下方式访问每个元素

and you would access each element as

 population[i][j] = initial_value;

请注意,在这种情况下,您不必显式取消引用population.现在,您可以处理种群大小不同的阵列,但仍然会遇到固定的染色体长度问题.

Note that you don't have to dereference population explicitly in this case. Now you can deal with arrays that have different population sizes, but you're still stuck with fixed chromosome lengths.

第三种方法是显式地将指向数组第一个元素的指针作为指向int的简单指针,并​​将其视为一维数组,并根据数组尺寸(作为单独的参数传递)手动计算偏移量:

A third approach is to explicitly pass a pointer to the first element of the array as a simple pointer to int and treat it as a 1D array, manually computing the offsets based on the array dimensions (passed as separate parameters):

init_pop(&population[0][0], pop_size, chrome_length);
...
int init_pop(int *population, size_t pop_size, size_t chrome_length)
{
  size_t i, j;
  ...
  population[i*chrome_length+j] = initial_value;
  ...
}

现在init_pop可以用于大小不同的int的2D数组:

Now init_pop can be used on 2D arrays of int of different sizes:

int pop1[10][10];
int pop2[15][20];
int pop3[100][10];
...
init_pop(&pop1[0][0], 10, 10);
init_pop(&pop2[0][0], 15, 20);
init_pop(&pop3[0][0], 100, 10);
...

编辑:请注意,上述技巧仅适用于连续分配的 2D数组;它不适用于主要维度和次要维度分别分配的动态分配数组.

EDIT: Note that the above trick only works with contiguously allocated 2D arrays; it won't work with dynamically allocated arrays where the major dimension and the minor dimensions are allocated separately.

这是一个方便的表,假设定义为int a[N][M]:

Here's a handy table, assuming a definition of int a[N][M]:


Expression     Type           Implicitly converted to
----------     ----           -----------------------
a              int [N][M]     int (*)[M]
a[i]           int [M]        int *
a[i][j]        int            
&a             int (*)[N][M]   

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

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