0

Why arr[0] in main function produce different output unlike arr[0] in user defined function in my code?Why arr[0] in main function giving output 5?

I have tried understanding it through pointers but still not getting.

void fun(int a[]){
a=a+1;
cout<<a[0]<<" ";}
int main(){
int arr[3]={5,10,15};
fun(arr);
cout<<arr[0]<<" ";
cout<<arr[1]<<" ";
return 0;}

I expect output to be 10 10 15,but the actual output is 10 5 10
Yash Mehta
  • 23
  • 2
  • 1
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Jan 25 '19 at 21:03
  • 1
    Why do you expect the output to be different? – SergeyA Jan 25 '19 at 21:05
  • Did you mean `*a = *(a+1);`? The expression `a=a+1;` is functionally equivalent to `a=&(a[1]);`. – François Andrieux Jan 25 '19 at 21:06
  • @FrançoisAndrieux I think he expects the change in `a` to also change `arr`. – john Jan 25 '19 at 21:07
  • 1
    It seems like you might be trying to reduce the size of `arr` in `fun` by "dropping" the first element. Arrays cannot be resized in c++. You would need to first pass the array by reference, but then there would still be no way to "move" `arr` to the second element. – François Andrieux Jan 25 '19 at 21:08
  • No It is a=a+1; – Yash Mehta Jan 25 '19 at 21:09
  • `a=a+1` increases the pointer value of 4, because `sizeof(int)` is 4. – Sandburg Jan 26 '19 at 14:59

3 Answers3

1

In your function a is a pointer. In your program a points to the first element of arr. You increment a so now it points to the second element of arr. So far so straightforward.

But the change in a has no effect at all on arr. arr is still an array of three elements and nothing that happens in fun changes that.

john
  • 85,011
  • 4
  • 57
  • 81
  • when a is passed means the address of first element is passed So why it is not reflecting in main function? – Yash Mehta Jan 25 '19 at 21:24
  • @YashMehta Because that's how C++ works. If you pass a **value** to a function then any changes to that value only happen inside the function. It's different if you pass a **reference** but there are no references in your program. – john Jan 25 '19 at 21:42
1

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.

Elijah Dayan
  • 442
  • 6
  • 19
  • 1
    [Arrays are not pointers](https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer). `arr` is not a pointer. – François Andrieux Jan 25 '19 at 22:23
  • No they are not. But built-in arrays get "implicitly" converted to pointer in most cases. I'd refer to "C++ primer" chapter 3.5 for more details. – Elijah Dayan Jan 25 '19 at 22:31
  • So then Why in main function arr[0] is 5 and not 10 – Yash Mehta Jan 26 '19 at 09:18
  • This is irrelevant to main function's scope. The pointer was advanced within the local scope of fun function. This will impact only the cout inside fun function's scope. – Elijah Dayan Jan 26 '19 at 13:10
  • Arrays are not copied in a function parameter argument. Only array pointer is "copied". A copy makes another distinct pointer inside the fun function's scope that points to same object. Any changes to that pointer is the change only to the local copy. The only exception could have been if the parameter was passed by reference. – Elijah Dayan Jan 26 '19 at 13:14
  • 1
    @RafsanMobasher That's true, but my comment was because the statement *" `int arr [n];` is actually `int *arr;`"* is incorrect and propagates the myth that arrays are pointers. – François Andrieux Jan 26 '19 at 14:16
  • Ok I see your point. I should have said "implicitly converted" rather than " is actually". Arrays are not pointers but built-in arrays really do get implicitly converted to pointers – Elijah Dayan Jan 26 '19 at 14:41
0

Your code creates the array: a[0]=5, a[1]=10, a[2]=15. When you call fun(arr) it passes in the array (a pointer to the array which is a pointer to the first value in the array: 5). In fun the "a=a+1" moves the pointer to point at the second value the array: 10. Printing a[0] prints out 10 because that is what the array points to. The values in the array itself are not changed. When you return you print out a[0] and a[1] where are 5 and 10. In main the array variable a has not moved.

Brian Walker
  • 8,658
  • 2
  • 33
  • 35
  • when a is passed means the address of first element is passed So why it is not reflecting in main function? – Yash Mehta Jan 25 '19 at 21:28
  • 1
    In main there is a variable called "arr" that has a value. When "fun(arr)" is called a copy of the value is passed to the function. In the function the copy of the variable (now called a) is changed. This does not change the value that is in main. If you passed a pointer to the variable to fun then you could change the value of the variable used in main. – Brian Walker Jan 25 '19 at 21:55