-3

In the following program, the value of the first element of array x prints as zero after passing the array as parameter to some function which modifies its parameters. The same does not happen to the int variable y, a modification in another function goes unnoticed in the calling function. Thus I was expecting the array to retain its values before the function call, just like it happens with y. Why is the array changed while the variable is not?

void func1 (int x[]){
    x[0]=0;
}
void func2(int y){
    y=0;
}

int main(){
    int x[]={7}, y=8;
    func1(x);
    func2(y);
    cout << x[0] << y;
    return 0;
}

output:

08

expected:

78
Bathsheba
  • 220,365
  • 33
  • 331
  • 451
PravinKanna
  • 19
  • 1
  • 4
  • Why is this "too broad"?! It's basically just about the syntax `type name []` as function parameter... – Daniel Jour Apr 30 '17 at 05:49
  • 1
    I tried to improve the question. Only thing missing: is this about C or C++? @PravinKanna – Daniel Jour Apr 30 '17 at 05:58
  • @PravinKanna In the `func2(y)` the variable is pass by value instead of reference there for the function will create a copy of the passed variable inside it, and than populate that copy variable instead of the passed one to it. So to make it work like `func1()` you need to do something like this `void func2(int &y) { y=0; }` The reference is like the address of the variable in the memory. As for the first function please referred to this post here http://stackoverflow.com/questions/14312970/arrays-passed-by-reference-by-default – Ali Niaz Apr 30 '17 at 06:13
  • Execute the code below , this will help you . . `#include using namespace std; void func1 (int x[]){ cout< – roy May 01 '17 at 19:39

3 Answers3

1

The parameter int[] is exactly the same as int*, a pointer to an int. The array passed to the function decays to such pointer to the first element of the array, thus dereferencing it through the subscript operator and modifying the int pointee results in the original being modified.

The Techel
  • 814
  • 8
  • 13
1

for you information i am putting values in comment in front of every line

void func1 (int x[]){
x[0]=0;//x here has same address as in main function so changes can be //directly apply to the original value of x array
//x[0]=7 was passed in this function is now modified by have a value x[0]=0

    } 
void func2(int y){
y=0;//but y is a different variable in this function it is not the same y in main function so if you change its value it does not mean that you are changing the value of y in main function so it does not give you the expected output
// it is due to local variable concept y in func2 is different and y in main is a different so all you have to do is to pass address of y variable so that if you want to change any thing is y will directly change its value in main function
}

int main(){
int x[]={7}, y=8;
func1(x);
func2(y);
cout << x[0] << y;
return 0;
 }
1

Array use contiguous memory location to store data. When you are calling func1(int x[]) then the previous value changing with value given by function and the location remaining same, so
in main function

x[0]=7

after function call

x[0]=0   

thats why your array value getting changed.
And for the variable you didn't return anything thats why no change for it. So 08 is correct output for this case

unreleased
  • 720
  • 1
  • 9
  • 21