Newbie dot Org HomePage
Visit one of our web buddies
Xxaxx's Xperimints #11
An Idiot's Guide to Pointers
Playing with C++

Please be advised that this title of this page has a double meaning. 1) The guide is addressed to folks who are new to the concepts of Pointers. 2) The guide is written by a folk who is new to the conepts of pointers. You have been forwarned.

Near as I can tell in C++ declared entities such as functions and variables are ways of calling for stuff. For example:

char c;

This statement defines, declares, makes, creates, brings into being, whateveryouwanttocallit, a variable with the name c. This variable with the name c is of type character. That means that whenever in the program, especially on the right side of the equal (=) sign, you call the name 'c' the program will fetch a character.

Instead of "char c;" let's use the declare before for c:

char* c;

This declares a name c which when called will bring forth a "pointer to a character". As far as I can tell a pointer is an actual memory location. The type of a pointer tells the compiler how to interpret the information found at the memory location.

To avoid confusion in one's programming it might be a good idea to use a convention (i.e. habit) of using slightly different names for names which call forth stuff and names which calls forth pointers to stuff.

char c;
char* c_ptr;

Here using the name "c" will call forth a piece of char stuff. And, using the name "c_ptr" will call forth a memory location of the piece of char stuff.

Consider the following: char c;
c = 'a';
char* c_ptr;
c_ptr = &c;

First before we go too far let's define this operator "&". When & is put in front of a name it tells the compiler to get the address (memory location) of the stuff referred to by the name.

Okay that said let's look at what the above does.

"char c;"
tells the compiler to create enough memory space for a character. It also tells the compiler to name the character to be stored at this memory location 'c'.
c = 'a';
tells the compiler to stuff the character 'a' into the memory location so that now the name 'c' when uttered will bring forth the character 'a'.
char* c_ptr;
tells the compiler to set aside a piece of memory (I think a DWORD) and call it c_ptr. This piece is memory is to hold the type of information which will be a pointer to a character. This means that the memory location referenced by c_ptr will be a DWORD integer which will be itself an address to another part of memory which is to be a character.
c_ptr = &c;
tells the compiler to grab the memory address of the character named 'c' and stuff that number into the memory location named c_ptr.
I suppose if you wanted you could define a pointer which would hold the pointer to a pointer of a character and other such.

Getting back to our intro to pointers let' recap from the above. If we utter the name 'c' the compiler/program will return a character -- 'a' in this case. If we utter the name 'c_ptr" the compiler/program will return a number which is the memory address where a character can be found.

But, given a pointer to a character how does one retrieve the character?

This is done through the '*' operator.

Consider the following:

char c; declare a variable 'c' which is a character.
char* c_ptr; declare variable 'c_ptr' which is a "pointer to a character"
char d; declare a variable 'd' which is a character.
d = 'a'; stuff the character 'a' into the variable d
c = d; call out the name 'd', have the compiler return whatever character is referenced by the name 'd'. Then stuff that character into the variable 'c'.
c_ptr = &c; apply the operator '&' on c to grab the memory address of 'c'. Then stuff this number (memory address) into the variable called 'c_ptr'.
*c_ptr; apply the operator '*' to the variable c_ptr grabbing the stuff which is pointed to by the memory address (number) stored at c_ptr. Then ..... let it sit there and do nothing.

Admittedly we could have done something more creative with the value returned by *c_ptr such as print it to screen or something. But you get the idea I hope.

Table 13-2 lists the operators used in conjunction with pointers.

 

Table 13-2 Pointer Operators

Operator

Meaning

*

Dereference (given a pointer, get the thing referenced)

&

Address of (given a thing, point to it)

The operator ampersand (&) returns the address of a thing which is a pointer. The operator asterisk (*) returns the object to which a pointer points. These operators can easily cause confusion. Table 13-3 shows the syntax for the various pointer operators.

 

Table 13-3 Pointer Operator Syntax

C Code

Description

thing

Simple thing (variable)

&thing

Pointer to variable thing

thing_ptr

Pointer to an integer (may or may not be specific integer thing)

*thing_ptr

Integer

Pointer Tutorial from book Practical C Programming (Nutshell Handbook) by Steve Oualline (Editor), Andy Oram (Editor)