Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between parameter types

Tags:

c

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

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


void fun(int (*a)[3])
{
   //do some task
}
like image 267
Green goblin Avatar asked Jun 20 '12 08:06

Green goblin


1 Answers

Nothing, [] is just syntactic sugar for a pointer.

Here's a simple test case to show that there's 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
}
like image 97
Emil Vikström Avatar answered Sep 27 '22 19:09

Emil Vikström