#include<iostream>
using std::cin;
using std::cout; using std::endl;
void test (int *x)
{
*x = 5;
}
int main()
{
int y;
cin >> y;
test(&y);
cout << y << endl;
return 0;
}
The relationship between x and y, is it like *x = &y?
Okay I don't get it now, can you please explain what is going on with the code?
May be I fail to see what you mean?
1. y is assign a value from standard input (cin).
2. Next y will be overwritten with the value 5 (test()).
3. Finally y will be written to standard output (cout).
Sorry I shouldn't have been so vague and actually explain. This is my thought process so far.
So y is assigned a value, then that value is used as an argument for pointer x.
test(&(y)) //What I think y is getting treated as at the moment.
In the function, pointer x gets dereference and is assigned the value of 5.
Since y is the address of pointer x, and when a pointer is dereferenced it is also accessing the object which it is pointing to, y is also assigned (overwritten) the value of 5 (what I understand about pointers, address and dereference from vlad's code).