Reference & Pointer issue C++

Started by
6 comments, last by Zahlman 13 years, 8 months ago
Hey,

I have a script where i simply set a variable to a number then run two functions that display the reference of that variable.. and then the pointer.

How ever the pointer fails but the reference works so im not sure if i have my syntax correct or misunderstood the tutorial i read as i got a little confused... but i tried any way and got this as my script

When i run the script the reference displays 1.2 and then it just says Press any key to continue so my PrintPlusTwo function failed.

My error is:
'PrintPlusTwo' : cannot convert parameter 1 from 'double' to 'double *'

Not sure what that suggests as i was told a * retrives the pointer location.

#include <iostream>using namespace std;void PrintPlusOne(double& drNumber) { //reference	std::cout << drNumber << std::endl;}void PrintPlusTwo(double* dpNumber) { //pointer	std::cout << *dpNumber << std::endl;}void main(){	double dOnePointTwo = 1.2;	PrintPlusOne(dOnePointTwo);	PrintPlusTwo(dOnePointTwo);	cout << dOnePointTwo << endl;}



Hope you can help. (ive been learning for 2 days so far)
Advertisement
PrintPlusTwo(dOnePointTwo);

Should be
PrintPlusTwo(&dOnePointTwo);

As dOnePointTwo is a double and the function signature requires a pointer to a double.
Hmm tried that - it gave the same answer as the reference =/



My full script + result.
It's supposed to give the same answer (going by the code).

You're not "display[ing] the reference of that variable.. and then the pointer". You're displaying the value bound to the reference, and the value stored at the address in the pointer (which is the same value).

This bit of code gets the address of your variable: "&dOnePointTwo" (i.e. it gets a pointer to that variable).
This bit of code 'dereferences the pointer', which gives you the original variable again: "*dpNumber" (i.e. it fetches the value stored at the memory address stored in the pointer).
Moomin is correct. But, since you're not happy with the results (they are working exactly as coded), I assume you're expecting a different result? Are you tryint to get the memory address that the pointer is pointing to? If so, you will need to remove the asterisk from the PrintPlusTwo function, like so...

void PrintPlusTwo(double* dpNumber) { //pointer	std::cout << "Pointer is " << dpNumber << std::endl;}


Maybe you can describe what you're expecting, when you run the code.
Oh yeah okay i removed the asterisk and it did what i was hopeing for. Guess i misunderstood my tutorial !

So is this currently getting the memory address of the reference or the original variable?

Also in my first mistake where i displayed the pointer and reference instead of memory address that the pointer point to & reference...that means the pointer is the same as the reference yet have different names? Seems a bit unusual.
Quote:Original post by thefollower
So is this currently getting the memory address of the reference or the original variable?


It is printing the memory address from the original dOnePointTwo variable. You can see this for yourself, if you just use the "address de-reference" character (&) in your cout statement, like so...

cout << "Original value is " << &dOnePointTwo << endl;


It will give you the same address that your PrintPlusTwo function prints.

Also, this stackoverflow page can explain much better than I can, the differences of pointer and reference types.
There are several things you have to understand here.

In the function signature, 'double *' means "I accept a pointer to a double". 'double &' means "I accept a reference to a double".

To pass a reference to a double, you just pass the double. To pass a pointer to a double, you have to create that pointer.

In an expression, "&x", where x is a double, means "the address of x". This value is a pointer to a double. It points to x. You cannot do "& &x", because "&x" does not have an address (it is a temporary value that isn't necessarily being "stored" somewhere that can be pointed to). "*y", where y is a pointer to a double, means "the thing pointed to by y". This value is, basically, a reference to a double. It refers to y. So you can do "*y = 42.0", and actually change the double that "y" is pointing at. (C didn't have references, but it effectively worked the same way. I guess that technically it isn't creating a reference in C++, either, but it's the easiest way to describe the effect accurately.)

To call PrintPlusTwo, we pass "&dOnePointTwo" so that the function has the pointer that it requests. The pointer points to the main function's variable called dOnePointTwo. If we output the pointer "dpNumber" directly, we see the address of dOnePointTwo (a number that indicates where it is in memory). If we output "*dpNumber", then we are outputting the actual value of dOnePointTwo, because we have created a reference to it

This topic is closed to new replies.

Advertisement