Help me understand pointers in C++

Oct 27, 2007
17,009
5
0
I've been studying Java for a year so I have no problems with reference variable but I'm getting all mixed up with pointers. It's the syntax that is mixing me up and I'm hoping someone here can correct my thinking.

Here's an example of some code from the book I'm studying from (sorry about the formatting, code box is broken):
void main()
{
double input = 12.4

round (input);
cout << "Rounded value: " << input;
}

void round (double &num)
{
double frac, val;
frac = modf(num, &val);

num = val;
}

Here's how I'm thinking about the code. The main method declares a double variable and assigns the value 12.4 to it. The round method takes a reference variable as a parameter, so when input is passed in as a argument the method actually takes &input, the memory address of input as a parameter. All of that is fine so far.

The modf() function is a standard library function whose signature looks like
modf(double num, double *i)
so the method takes a double and a pointer as arguments. I don't understand why &val is passed as the pointer argument. To my way of thinking, &val is not a pointer, it's just a memory address. How can a memory address be passed into an argument that requires a pointer?

I'm sure there's some fundamental problem with my way of thinking about pointers. Any help and tips would be greatly appreciated.
 

Sc4freak

Guest
Oct 22, 2004
953
0
0
A pointer is just a variable that contains a memory address. The & operator gets the memory address of a variable, which is then passed (as a pointer) to the modf function.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
/agree sc4freak

References and pointers in C++ are basically the same thing with different syntax. References are often preferred because they can be used without explicit dereference operators (*). It does get rather confusing because the address-of operator, &, is used to declare reference variables, and the dereference operator, *, is used to declare pointer types, but & is also used to create pointer values.

That is... double &x is declaring x as a reference to a double ("double &" is a type), but just &x is taking the address of x as an operand ("&" alone is an operator).
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
The only thing I would add to what the others have said is that it's not ok to simply think of references as pointers with another name. Pointers are lvalues containing a scalar quantity, i.e. the address of a location in memory. You can perform arithmetic on pointers, for example, irrespective of the location pointed to. As far as I recall there are no independent operations on references other than intialization. In other words a reference is a propagated name of a specific type, not a modifiable lvalue. It's been years since I immersed myself in the design of C++, but I think the basic justification is that they are safer than pointers, since they must be initialized, and having been initialized cannot be changed.
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
two basics know:

& address of
* contents of address

int x;
void f (int *a) {
*a = 2;
}

main() {
x = 4;
f(&x);
printf(?%d?, x
);

output would be 4

see how we put the address of x in f() then changed the contents of that memory location to 2 (*a = 2)
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: net
two basics know:

& address of
* contents of address

int x;
void f (int *a) {
*a = 2;
}

main() {
x = 4;
f(&x);
printf(?%d?, x
);

output would be 4

see how we put the address of x in f() then changed the contents of that memory location to 2 (*a = 2)

Ummm... I'm going to guess that the output would be 2. ;)
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,249
561
126
Yeah, I want to say it took me a week or two to finally have the "aahaa! moment" with pointers. Once you realize that it is just the runtime memory address for whatever you created the pointer to, life starts to make sense. Then you just need to understand the different syntax for quickly telling the compiler that you want to operate on the address itself, or the data contained at the address. Finally comes the fact that you will probably have to tell the compiler how to deal with the data contained at the address, since it might not be able to figure out how to treat the data, and so you need to "cast" the data as something.... its gets a lot easier once you use it a few times...
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Just think about pointers as integers that just happen to coincide with memory addresses of variables. If you where to do cout << somepointer; you would get a number that is just an address, nothing more. Just like I could write down your home address on a piece of paper, it isn't your home, it is just a pointer to where your home is.

when you dereference a pointer (IE add the * to the front of the pointer name) you are looking up the address and actually using that variable.

Hope that makes sense.
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Originally posted by: net

Okay I wrote this for you. If you have any questions about it please ask.

Thanks.

It shows what happens in memory according to your C code. (it applies to C++)

http://rafb.net/p/2mLTMq29.html

I get an error that document is not found on server. Any chance you can repost it? I am another one mainly familiar with Java, but I'm about to start studies in a program that is seemingly C/C++ focused, so I would love a nice refresher.