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.

3
  • 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) Commented Dec 6, 2012 at 14:27
  • 1
    @B_o_b: It is called VLA (Variable length arrays) which is supported C99 onwards (but not in C++) Commented Dec 6, 2012 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. Commented Dec 6, 2012 at 14:35

2 Answers 2

2

use

function(array)

instead of

function(array[10][x])

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

Sign up to request clarification or add additional context in comments.

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.