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;

 }
3
  • 1
    have a look at this stackoverflow.com/questions/5491789/… Commented May 25, 2013 at 9: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). Commented May 25, 2013 at 9: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<size_t size> void g(int (&arr)[size]), which lets you call it this way: g(array);. Commented May 25, 2013 at 10:08

4 Answers 4

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 *.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

-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.

2 Comments

I know, I know, it was just to set an example ^^.
The reason you state is mere speculation.
-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.

1 Comment

This is not true. "a declaration of an array return allawys a pointer" is plain wrong. The type of a in int a[10]; is very different from the type of a in int *a;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.