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;
}
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.
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.
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.
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.
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 *
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With