The difference between the types of parameters

What is the difference between the following two function definitions?
A 2D array is passed as a parameter.

void fun(int a[][3])
{
   //do some task
}


void fun(int (*a)[3])
{
   //do some task
}
+5
source share
3 answers

Nothing []- it's just syntactic sugar for a pointer.

Here is a simple test case showing that there is not even a difference in indexing:

#include <stdio.h>

void fun1(int a[][3]) { printf("%d\n", a[2][2]); }
void fun2(int (*a)[3]){ printf("%d\n", a[2][2]); }

void main() {
  int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  fun1(a);  // prints 9
  fun2(a);  // prints 9
}
+12
source

Nothing, Both are the same. Only for our purpose.

+3
source

There is no difference between the two. In C, when an array notation is used for a function parameter, it is automatically converted to a pointer declaration .

+2
source

All Articles