tags:

views:

210

answers:

5

Having some serious problems wrapping my head around how to pass a 2D array to a function in C - pointers are not my strong point!

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.

Currently I have

#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;
}

Which gives me the compilation error of "error: array type has incomplete element type"

Any help appreciated here!

+3  A: 

You need to tell the compiler all dimensions except the first, when passing arrays as arguments:

int init_pop(int population[][pop_size])
{ 
 ...
}

Yes, this means it's hard to make it completely dynamic and introduces a place where you have to repeat yourself.

UPDATE: I was confused, and had the requirement inverted. Fixed now.

unwind
It's kind of scary that this answer was accepted with 3 upvotes given the fact that it is completely wrong. You need to specify all dimensions except the *first*.
Robert Gamble
@Robert: Thanks, and fixed of course. I should have tested it, sorry.
unwind
A: 

Not an answer (is this homework?) but you might want to look here first. Then take a look here as well.

lorenzog
A: 

This is the problem:

int *population[][]

A multidimensional array is simply a block of continuous memory, and when you say foo[3][2], the compiler finds the right index by 3*last_dimension_size + 2, which means that it has to know the size of all the dimensions except the last one.

So that declaration is an error.


BTW-- There are several very complete discussion of issues related to multidimensional arrays in c already on SO. Try searching under both or either [c] and [c++]

dmckee
+6  A: 
John Bode
The most verbose answer, and +1 specifically for mentioning pointers-to-arrays (which seem like the best choice given requirements here).
Pavel Minaev
Well done, this deserves a nomination for "Best of StackOverflow"
Ed Griebel
A: 

For multidimensional arrays in C/C++ you have to specify all dimensions except the first.I am modifying your program to make it work correctly:

#include <stdio.h>
#include <stdlib.h>
#define pop_size 3
#define chrom_length 3

void init_pop(int population[][chrom_length]) {
 int i,j;
 for(i = 0; i < pop_size; i++) {
   for(j = 0; j < chrom_length; j++) {
   population[i][j] = rand() % 2;
   }
 }
}

/* For Checking */
void display (int population[][chrom_length]){
  int i,j;
 for(i = 0; i < pop_size; i++) {
   for(j = 0; j < chrom_length; j++) {
    printf("%d ",population[i][j]);
   }
   printf("\n");
    }
}


int main(void) {
 int population[pop_size][chrom_length];
 init_pop(population);
 display(population); /* For Checking */

 return 0;
}

If you not going to use global constants here is the correct way of doing it.

nthrgeek