1

So basically, what I'm trying to do is pass the array as an argument. I came up with such idea:

#include <stdio.h>
#include <stdlib.h>

int x;

void function(int array[][x]){
    //stuff here
}

int main(){
    x = random(10);
    int array[10][x];
    //initialize array
    function(array[10][x]);
}

I thought this should work, but it gives me a note:

expected ‘int (*)[(unsigned int)(x)]’ but argument is of type ‘int’

Any help would be appreciated.

simonc
  • 41,632
  • 12
  • 85
  • 103
eksponente
  • 49
  • 2
  • 9
  • surely the array declaration isn't valid either, as x is not known at compile time. Please correct me if I'm wrong, but you shold use malloc for this. `int* array = (int*)malloc(10*x)` – B_o_b Dec 06 '12 at 14:27
  • 1
    @B_o_b: It is called `VLA` (Variable length arrays) which is supported C99 onwards (but not in C++) – another.anon.coward Dec 06 '12 at 14:31
  • @another.anon.coward: I had a feeling I was about to learn something new. Thank you. Leaving comment for others like myself. – B_o_b Dec 06 '12 at 14:35

2 Answers2

2

use

function(array)

instead of

function(array[10][x])

you might want to read this post I made a few days back.

Community
  • 1
  • 1
Minion91
  • 1,911
  • 12
  • 19
2

You are passing the argument to the function incorrectly. It should be function(array) as pointed out.
Other observations:
1. Use of random() should probably be random()%10; and not random(10); unless its some non-standard function.
2. int main() should be either int main(void) or int main(int argc, char **argv)
3. You should return an int value from main say return 0 or return EXIT_SUCCESS

Additional note:
Please note that you may not be able to get the actual dimensions of the array in the function. Probably you should consider passing the dimensions of the array in case you plan to use it in the function.

Hope this helps!

another.anon.coward
  • 11,087
  • 1
  • 32
  • 38