Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between int * array and int array[] in a function parameter

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;

 }
like image 731
Alex Avatar asked May 25 '13 09:05

Alex


People also ask

What is difference between int array [] and int [] array?

There is no difference in these two types of array declaration. There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays.

Is int [] and int * Same?

Not at all. int * means a pointer to an integer in your memory. The [] bracket stands for an array. int a[10]; would make an array of 10 integers.

What is the meaning of int * array?

int *array; defines a pointer to an int . This pointer may point to an int variable or to an element of an array of int , or to nothing at all ( NULL ), or even to an arbitrary, valid or invalid address in memory, which is the case when it is an uninitialized local variable.

Can int * be used for an array?

You can use an array with elements of the numeric data type. The most common one is the integer data type (int array in Java). The following program illustrates the usage of the array with the int data type.


1 Answers

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

like image 83
Angew is no longer proud of SO Avatar answered Oct 20 '22 09:10

Angew is no longer proud of SO