-1

I saw some codes on the web and trying to figure out how this works. I tried to leave comments on each lines but I cannot understand why y[0] changes to 5555. I'm guessing y[0] might change to numbers[0], but why? x value is still 1. Well.. is this because y[0] = 1; has no int data type?

#include using namespace std;

void m(int, int []);
/*this code explains a variable m that m consists of two parts, int and int array*/

int main()
{
  int x = 1; /* x value is declared to 1*/
  int y[10]; /*Array y[10] is declared but value is not given*/
  y[0] = 1; /*Array y's first value is declared to 1 but data type is not given*/


  m(x, y); /*This invokes m with x and y*/

  cout << "x is " << x << endl; 
  cout << "y[0] is " << y[0] << endl;
  return 0;
}

void m(int number, int numbers[]) /*variable names in m are given, number and numbers.*/
{
  number = 1001; /*number has int 1001 value*/
  numbers[0] = 5555; /*This overrides y to numbers[], so y[0] =1 changes to numbers[0] = 5555.*/
}

/*This program displays 
 * x is 1
 * y[0] is 5005
 * y[0] value has changed but x has not.
 * */
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
Unknown Shin
  • 99
  • 2
  • 10
  • 2
    Arrays are homogeneous in C++. If `y` is an `int[]`, it can only contain `int`s. – Quentin Feb 05 '18 at 17:27
  • 2
    This was answered many times already. TL;DR: Function parameters are passed by value and changes made to them inside of functions are not visible outside. To circumvent that you pass pointers/references to functions, because changes to pointed/referenced values are not discarded. And `int numbers[]` is the same as `int *numbers` (*only when used as a function parameter*), which is a pointer. – HolyBlackCat Feb 05 '18 at 17:28
  • 2
    Also `y[0] = 1;` is not a declaration, it's an assignment. – HolyBlackCat Feb 05 '18 at 17:29
  • 1
    Btw, the "basic" tag is supposed to refer to the [BASIC](https://fr.wikipedia.org/wiki/BASIC) languages and is unrelated to the simplicity of your problem. – Caninonos Feb 05 '18 at 17:31

2 Answers2

2

I'm guessing y[0] might change to numbers[0], but why? x value is still 1.

Don't guess please. Your code works as commonly expected.

number = 1001; doesn't influence x in any way.
number is a local copy (as passed by value).

numbers decays to a pointer to the 1st element of the original array, thus it is changed outside the functions scope.

Well.. is this because y[0] = 1; has no int data type?

No, as explained above. y[0] actually is of type int.

  • @user4581301 Better? Added a quantum of politeness as well ;-) –  Feb 05 '18 at 17:53
  • Thank you for the link. Also you should mention to avoid the pointer decay to wrap the array in a structure. Probably his intended behavior. – Victor Padureanu Feb 05 '18 at 18:15
  • @Victor Well, [`std::array`](http://en.cppreference.com/w/cpp/container/array) serves that well already. No need to roll your own _structure wrapper_ for it. Should I mention it? –  Feb 05 '18 at 18:19
0

int numbers[] is almost equivalent to int* numbers in this situation. You are not passing the vector as an immutable object but as a reference. So both numbers ( the local variable from the function ) and y ( the local variable from main ) will be pointing to the same memory address.

Victor Padureanu
  • 604
  • 1
  • 7
  • 12