# TUTORIAL: Pointers and references

• Posts: 106
• Loc: UK

3+ Months Ago

## 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
Code: [ Select ]
int intValue = 15;

With the memory layout of
Code: [ Select ]
Memory location : Value
0x004 --------- : 15
1. Memory location : Value
2. 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
Code: [ Select ]
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
Code: [ Select ]
intPointer = &intValue;

and now the memory example for 0x010 (the intPointer) would be
Code: [ Select ]
Memory location : Value
0x010 --------- : 0x004
1. Memory location : Value
2. 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
Code: [ Select ]
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.
• Brewmaster
• Posts: 6297
• Loc: Seattle, WA

3+ Months Ago

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.
• ٩๏̯͡๏۶
• Posts: 3423
• Loc: Richland, WA

3+ Months Ago

Quote:
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.
• Posts: 106
• Loc: UK

3+ Months Ago

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.
• Brewmaster
• Posts: 6297
• Loc: Seattle, WA

3+ Months Ago

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.
• Posts: 106
• Loc: UK

3+ Months Ago

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

## Post Information

• Total Posts in this topic: 6 posts
• Moderator: Tutorial Writers
• Users browsing this forum: No registered users and 2 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum