Reference

I am trying to learn how pointers work :)
This code work's perfectly:

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
#include <iostream>

using namespace std;

void offsetvector(double &x0, double &y0, double &x1, double &y1, double offsetX, double offsetY)
{
	x0 += offsetX;
	x1 += offsetX;
	y0 += offsetY;
	y1 += offsetY;
}


void printvector(double x0, double y0, double x1, double y1)
{
	cout << "(" << x0 << "," << y0 << ") -> (" << x1 << "," << y1 << ")" << endl;
}

int main()
{
	double xStart = 2.1;
	double yStart = 2.2;
	double xEnd = 4.1;
	double yEnd = 4.2;
	offsetvector(xStart, yStart, xEnd, yEnd, 1.0, 1.5);
	printvector(xStart, yStart, xEnd, yEnd);
}
But if i change offsetvector function to this:
...
	&x0 += offsetX;
	&x1 += offsetX;
	&y0 += offsetY;
	&y1 += offsetY;
...
or
&y1 = 20.0;
or
&y1 = &y0;


it doesn't work. Why?
Last edited on
You are actually using references (which is the preferred method of argument passing.) When you change offsetvector() to this:

1
2
3

&x0 += offsetX;


You are adding the value of offsetX to the address of x0, which I don't think will even compile. Your initial version has it correctly.
Thanks kppth for your fast response.

Yes, i understand that. Thanks.

But why this does not work?

&y1 = &y0;

actually making the address of y0 same as y1.

PS: Writing this I actually figure it out that you cannot have 2 values in the same address.
( this could be the reason:) )
Writing this I actually figure it out that you cannot have 2 values in the same address.

That's exactly right. 2 values at the same address means 2 variables at one memory location, which is inpossible, there simply is no space to do that.
Topic archived. No new replies allowed.