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.
I have the following code,
#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
Any suggestions?
Time for the usual spiel…
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
sizeofor 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?
The type of the expression
populationis “pop_size-element array ofchrome_length-element arrays of int”. Going by the rule above, in most contexts the expressionpopulationwill implicitly be converted to type “pointer tochrome_length-element arrays of int”, orint (*)[chrome_length].The type of the expression
&population, however, is “pointer topop_size-element array ofchrome_length-element arrays of int”, orint (*)[pop_length][chrome_size], sincepopulationis an operand of the unary&operator.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
the corresponding function definition should be
and you would access each element as
Note that this means
init_popcan only deal withpop_sizexchrome_lengtharrays; you can’t use it on arrays of different sizes.If you call the function as
then the corresponding function definition would have to be
and you would access each element as
Note that you don’t have to dereference
populationexplicitly in this case. Now you can deal with arrays that have different population sizes, but you’re still stuck with fixed chromosome lengths.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):
Now
init_popcan be used on 2D arrays of int of different sizes: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.
Here’s a handy table, assuming a definition of
int a[N][M]: