I want to pass a 2D matrix to a function in C which modifies the elements of the matrix and the modifications should be effective in the calling function.
How to achieve this in C.

All arrays, including multi-deminensional arrays, are passed by address, never by value. Passing a 2d array is similar to 1d array, the sedond deiminsion is optional.

void foo(int array[10][10])
{
  // blabla
}

or

void foo(int array[10][])
{
  // blabla
}

int main()
{
   int array[10][10];
   // now pass the array to the function
   foo( array );
   return 0;
}

What if the size of the matrix is variable one.how to define a function then.

All arrays, including multi-deminensional arrays, are passed by address, never by value. Passing a 2d array is similar to 1d array, the sedond deiminsion is optional.

void foo(int array[10][10])
{
  // blabla
}
 
or
 
void foo(int array[10][])
{
  // blabla
}
 
int main()
{
   int array[10][10];
   // now pass the array to the function
   foo( array );
   return 0;
}

In addition to the two choices I posted previously you could use the stars

void foo(int **array)
{

}

> void foo(int array[10][])
Should be
void foo(int array[][10])
It's only the left-most dimension which can be left empty.

> void foo(int **array)
Whilst this does allow variability, it doesn't allow you to do

int small[10][10];
int large[100][100];
foo( small );
foo( large );

There is no good way to write a single function which would accept small and large.
http://c-faq.com/aryptr/ary2dfunc2.html

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.