Introduction

Pointers and references are just ways of accessing data that is stored in memory. The basics of a pointer (*) are that is it will either store a NULL value or a value of a memory location where the actual data is. The reference (&) is the actual memory location address of a variable. I did do a function pointer tutorial (cpp-tutorials/tutorial-function-pointers-t101998.html) and this a follow on with what a pointer is.

Standard variable - memory layout

Lets say that at memory location 0x004 there was a value of 15. Well this memory location 0x004 could be called a integer value with a name of intValue, code example

int intValue = 15;

With the memory layout of

Memory location : Value
0x004  --------- : 15

Pointer assignment and value

A pointer to that variable at 0x004 (intValue) will have another memory location lets say at 0x010 so a code example would be

int *intPointer= NULL;

best practice to set to NULL, and if you want to assign the pointer to the intValue you will need to give the pointers memory location value the address of the intValue memory location as

intPointer = &intValue;

and now the memory example for 0x010 (the intPointer) would be

Memory location : Value
0x010  --------- : 0x004

It is the value of the actual memory location and not the value of 15, since that the value of intValue. To actually get the value you need to dereference the pointer with the * as below

cout << "pointer value = " << intPointer << " the value = " <<  *intPointer << endl;

That will output
pointer value = 0x004 the value = 15.

Conclusion

A pointer is a easy concept and also easy to mess up since you are playing with memory locations, but allot of fun at the same time.

I am going to do a tutorial on function parameters with pointers and references to memory locations and what is the reason for them.

I do really like to have any feedback regarding any tutorial/post, just reply or PM me.. glad to help, better to share knowledge.

This page was published on It was last revised on

0

5 Comments

  • Votes
  • Oldest
  • Latest
SP
106 4
Commented
Updated

Is this a C or C++ tutorial? If C++, you may want to remove "and references" from the title as it makes no mention of C++ references, which are unique beasts in and of themselves.

add a comment
0
SF
25 0
Commented
Updated

A pointer is a easy concept and also easy to mess up since you are playing with memory locations, but allot of fun at the same time.

Easy for some, I believe my prof spend a good 2 weeks on pointers because people couldn't grasp the concept of a variable not really storing data, but holding (representing) the location of it.

add a comment
0
Commented
Updated

Spork, yep.. .it was a harp back to the old C days of references as such. Even though the "&" does the same task of the address in memory, it is classed more as
"A refernce is an alternative name for a object"
as quoting Bjarne Stroustrup.

How do I delete the and references from the title ? or do I resubmit the tutorial with just copy and paste without quoting a reference at the top.

add a comment
0
SP
106 4
Commented
Updated

Heh, Bjarne is a good guy. Good taste in beer 😉

One thing you might want to do with this tutorial is mention that *** and &** are both operators. The "*" operator dereferences a pointer, that is, it accesses the memory location pointed to by a memory address stored in a pointer. The "&" operator obtains the memory address in which a given variable's data resides.

add a comment
0
Commented
Updated

You have meet up with Bjarne ? I like his books.

add a comment
0