I was having some trouble related to a code.
I wanted to pass a 2-D array as argument to a function,but I'm getting an error saying 'Expression Syntax in function Main'.
I am trying to do it like this :->

void input(int **a[][10],int b,int c)
    int i,j;
    printf("\nEnter the elements of the Array :-> ");
    for (i=0;i<b;i++)
        for (j=0;j<c;j++)
void main()
    int a[10][10],b[10][10],c[10][10],i,j,k,choice,m,n;
    printf("Enter the no. of rows and cols for A and B :-> ");

So,I wanted to know where am I wrong.
At some place I read that in C 2-D arrays can't be passed as arguments.
If so,how can I pass them??

Thanks In Advance

Recommended Answers

All 4 Replies

In C arrays can't be passed as arguments (or return values directly either) you can only pass pointers. Additionally C does not have 2 (or multi) dimension arrays when you declare int a[10][10] you are not declaring a 2 dimensional array but rather an array of arrays (a subtle but important distinction).

For any array using its name without an index provides a pointer to the first element in the array so for an array of arrays using the name provides a pointer to an array, the type of a is int (*)[10] and that is what you want to pass to your function which means that the prototype needs to be void input(int a[][10], int b, int c) or void input(int (*a)[10], int b, int c); and when you call it you can just pass the name of your array of arrays input(a, m, n);

Modify "input" function argument as (int a[10][10], int b, int c)

and call it by input(a, m, n); or input(&a[0][0], m, n);

That will do I suppose.

In C, ALL parameters are passed by copying - NOTHING ELSE. When you copy an address (pointer), you have a way of affecting the original variables that are pointed to by the address - so it's a pass by reference, in it's effect.

You pass a 2D array to a function, by just using the name of the array, in the call to the function:


In your function parameter list a 2D int array like this:

void myFunction(int arrayName[][NumberOfColumns]) {
   //other code in here


Note that the number of rows (in the first [], is not needed. All other dimensions size, need to be included, however.
NO *'s are needed, at all, which seems to be the confusing factor.

Arrays in C are ALWAYS passed by the address (which is the name of the array most commonly). The receiving function has to have the greater share of explicit info about the array, if it is to be treated as an array - instead of just as a "something" at a certain address. (which is also possible, but not recommended - especially for beginners). In this latter type, you are working explicitly with pointers (addresses) and offsets, which makes it not as easy to understand.

thanks guys,
doing it like



Thanks for all the help...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.