Note: We cannot have parameter of array types since arrays are not copied. Parameter of array types automatically get converted to pointer. That doesn't mean array types always get converted to pointer types. Based on the context, array types "sometimes" get converted to pointer types implicitly. C++ primer book has some really good examples on this topic:
// despite appearances, these three
// declarations of print are equivalent
// each function has a single
// parameter of type const int*
void print(const int*);
void print(const int[]);
// shows the intent that the function
// takes an array
void print(const int[10]);
// dimension for documentation
//purposes (at best)
When you do a = a + 1;
you are actually advancing the array pointer inside fun function's local scope. Arrays are not copied in function argument. So only array pointer will be "copied". However, the local array pointer will be a distinct pointer that will point to the same object. You then advance that pointer which makes no impact to your actual array pointer. I think you are lacking knowledge about pointer arithmetic, and implicit pointer convertion behaviors of built-in arrays.
int arr [n];
gets implicitly converted to int *arr;
. Note that size n
inside subscript is actually part of declaration for built-in arrays. Built in arrays implicitly create pointer to the first element. Then you say a= a +1;
This is equivalent to saying your "local copy" of array pointer to point to arr[1]
. You do all these inside fun function's pointer, which is a copy of your actual passed argument, and it is itself a distinct pointer. Any changes to that pointer has no impact to actual array pointer object as you intended.
You can *arr = *arr + 5;
or arr[0] = arr[0] + 5;
to actually add 5 to your first element to make it 10.