array as parameters

Hi guys the code I wrote does not do what I thought it would do,I was expecting printRay to print nothing or garbage because I thought since I was passing the array ra in by value(copy) to fillRay that it would just make a copy and not fill the array ra but yet it feels the array I thought that if I wanted to fill the array and be able to use it I would have had to pass by reference?


thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>

using namespace std;



void fillRay(int x[],int sizeOf){


     for(int i = 0; i < sizeOf; i++){


           x[i] = i;

     }
}

void printRay(int x[],int s){

     for(int i = 0;i < s; i++){

         cout << x[i] << endl;

     }
}

int main()
{
    int ra[3];
    int s = 3;

    fillRay(ra,3);
    printRay(ra,3);

}
closed account (E0p9LyTq)
Simplified explanation to follow:

Fixed sized arrays decay to a pointer when passed as a parameter into a function. So when passing an array you are not making a local copy, your function can modify the array.
Topic archived. No new replies allowed.