6

Is there any difference between

foo(int* arr) {}

and

foo(int arr[]){} ?

Thanks

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
VextoR
  • 5,087
  • 22
  • 74
  • 109

4 Answers4

12

No, there is no difference between the two.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
5

There is no difference for the C compiler. There is a difference for the programmer that reads the code though.

Here, arr is a pointer to an integer (possibly for returning the result from the function):

foo(int* arr) {}

Here, arr is a pointer to the first integer in an array (possibly for passing a list of numbers in and/or out of the function):

foo(int arr[]) {}

Also, specifying the return type of the function would help.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
1

The semantic is the same, but for an external programmer, it is easier and immediate to understand: the second function takes an array as argument. It could not be as immediate for the first one.

Julio Guerra
  • 5,523
  • 9
  • 51
  • 75
-1

You will have to dereference values to the first one...

Martin Milan
  • 6,346
  • 2
  • 32
  • 44
  • nope. well it depends, you can dereference the second one if you want: `*(array + 3)` is the same as `pointer[3]`. – BlackBear Mar 30 '11 at 20:21
  • Well, I have two functions: int function(int arr[]); int function2(int* arr); For testing, I have this array: int arr[5] = {1,1,1,1,1}; In Visual Studio 2010 I can do this: function(arr); function2(arr); Even though I didn't pass in a dynamically allocated array into function2, it still allowed me to do it. Why is this? And from doing benchmarks, both functions have identical results, so if dereferencing does happen, it's negligible. – leetNightshade Mar 30 '11 at 20:26
  • You can even write 1[arr] for both! – Flexo Mar 30 '11 at 20:37