TUTORIAL: Pointers and references

genux

Graduate

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

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.

spork

Brewmaster

Posts: 6299

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.

SpooF

٩๏̯͡๏۶

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.

genux

Graduate

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.

spork

Brewmaster

Posts: 6299

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.