14

I have seen that the array values ​​will change if the function parameter is "int arr[]" or "int * arr". Where is the difference?

int array[]:

void myFunction(int arr[], int size) {

    for (int i = 0; i < size; ++i)
        arr[i] = 1;
}

int * array:

void myFunction(int * arr, int size) {

    for (int i = 0; i < size; ++i)
        arr[i] = 1;
}

Both functions change the array values.

int main(){

     int array[3];

     array[0] = 0;
     array[1] = 0;
     array[2] = 0;

     myFunction(array, 3);

     return 0;

 }
Alex
  • 480
  • 1
  • 5
  • 15
  • 1
    have a look at this http://stackoverflow.com/questions/5491789/any-difference-between-fooint-arr-and-fooint-arr – Rohan May 25 '13 at 09:29
  • 7
    No difference. At all. _In a function parameter list_, `int arr[]` is nothing but an "alternative" writing for `int* arr`. (And that's why you can see `int main(int argc, char* argv[])` or `int main(int argc, char** argv)`.) Even if you were to put a number inside the brackets!: in `void f(int a[10])` the `10` is completely **ignored**, which means `void f(int a[])`, i.e. `void f(int* a)`. – gx_ May 25 '13 at 09:54
  • 1
    And as said in answers, for the call `myFunction(array, 3);` your array is implicitly converted to a _pointer_ to its first element, and that pointer is passed to the function (with either writing of the parameter), as if you had called `myFunction(&(array[0]), 3);`. One way to really pass the array (actually, a _reference_ to it) is to write a function like `template void g(int (&arr)[size])`, which lets you call it this way: `g(array);`. – gx_ May 25 '13 at 10:08

4 Answers4

9

There is no difference. Both functions types (after adjustment) are "function taking a pointer to int and an int, returning void." This is just a syntactic quirk of C++: the outermost [] in a function parameter of non-reference type is synonymous with *.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

Different ways to express the same thing.You are simply passing an array to functions by pointer.

Tcz
  • 661
  • 5
  • 18
-2

when you pass an array to functions it implicitly passes it by pointer. because if there is many elements in array pass by value copying it is a Huge overhead. even-though it looks different its same.

you can't use both functions at same time. if you do its compile time error

 error: redefinition of 'void myFunction(int*, int)'
 it is already defined.
Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
-3

There are no difference. In fact, a declaration of an array return allawys a pointer. Only, when you specify in the declaration that the variable is an array (ie. with []), it is not necessary to specify that it will be a pointer, because this is made implicitly. But when you do not specify this, you must declare it as a pointer if you went affect it a table variable, or a result of "new []". The use of pointers of table has an interest only for a dynamic allocation, when the array size is not known on design-time.

HomDhi
  • 1