I fixed your code and also printed some addresses for your understanding.
Old code:
#include <iostream>
using namespace std;
void citire(int& n, int* a)
{
cin >> n;
a = new int[100];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "In citire Address of a = " << &a <<endl;
cout << "In citire Address of n = " << &n <<endl;
}
int main()
{
int n, *a;
cout << "Address of a = " << &a <<endl;
cout << "Address of n = " << &n <<endl;
citire(n, a);
for (int i = 0; i < n; i++)
{
cout << *a << " ";
}
return 0;
}
Output:
Address of a = 0x7ffee5ab6b90
Address of n = 0x7ffee5ab6b98
5
1 2 3 4 5
In citire Address of a = 0x7ffee5ab6b50
In citire Address of n = 0x7ffee5ab6b98
0 0 0 0 0
Note the addresses of variables a
and n
in main()
and inside citire()
function.
Modified code:
#include <iostream>
using namespace std;
void citire(int &n, int* &a)
{
cin >> n;
a = new int[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "In citire Address of a = " << &a <<endl;
cout << "In citire Address of n = " << &n <<endl;
}
int main()
{
int n, *a;
cout << "Address of a = " << &a <<endl;
cout << "Address of n = " << &n <<endl;
citire(n, a);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
delete [] a;
return 0;
}
Output:
Address of a = 0x7ffee0eb1b90
Address of n = 0x7ffee0eb1b98
5
1 2 3 4 5
In citire Address of a = 0x7ffee0eb1b90
In citire Address of n = 0x7ffee0eb1b98
1 2 3 4 5
Note in this case, both n
and a
's addresses are same inside the function as well as in main
. Since they were passed by reference
, the actual variables (just aliased) were passed with their memory locations intact, any change done to them is also reflected in main
as well.
Key takeaway: Even pointers
are passed by value
to functions unless you force it to pass by reference
!
Edit (Array code):
#include <iostream>
using namespace std;
void citire(int& n, int a[])
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "In citire Address of a = " << &a <<endl;
cout << "In citire Address of n = " << &n <<endl;
cout << "In citire Address of first element of a = " << &a[0] <<endl;
}
int main()
{
int n;
int a[100];
cout << "Address of a = " << &a <<endl;
cout << "Address of n = " << &n <<endl;
cout << "Address of first element of a = " << &a[0] <<endl;
citire(n, a);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
return 0;
}
Output:
Address of a = 0x7ffeef1cda00
Address of n = 0x7ffeef1cd9f8
Address of first element of a = 0x7ffeef1cda00
5
1 2 3 4 5
In citire Address of a = 0x7ffeef1cd9b0
In citire Address of n = 0x7ffeef1cd9f8
In citire Address of first element of a = 0x7ffeef1cda00
1 2 3 4 5
Note that in case of array
s, even though they are passed by value, in a sense that the address of a
changes in function calls, the address of the region (i.e., of 100 integers as you have statically declared), basically the address of the first element of the array (&a[0]
) do not change. Think of it like this:
- You have a black box, inside which you store a number which is the address of
a[0]
which is the actual region a
points at.
- Now say the black box itself has an identification number
XXX
.
- During function call, you put the contents of the black box
XXX
, into another black box, say with an identification number YYY
.
- Undestand that
YYY
has the same pointed region i.e, &a[0]
.
- You ship the new
YYY
black box to function which uses and manipulates the data in the region which starts at &a[0]
.
So you get the result reflected in main()
. Also note that array
and pointer
types are not same.