Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / Objective C

A Prelude to Pointers

Rate me:
Please Sign up or sign in to vote.
4.42/5 (49 votes)
23 Jul 20036 min read 234.3K   1.1K   105   74
A quick reference to pointers and pointer operations

Introduction

Pointers are undoubtedly a daunting topic of discussion for people just starting out in the C++ lifestyle, which prompted me to write this brief article. Note that this is not a full-featured discussion on pointers, but a quick reference to commonly asked questions about their use and what they really mean. This is also a nice primer to Andrew Peace's[^] article: A Beginner's Guide to Pointers[^], where he presents an excellent discussion on the topic.

Philosophy

Like I previously stated, my intent is not to give the origin of the pointer universe, but to provide a brief example of how pointers are used, and in the process hopefully clear up some lingering questions posted in the forums and similar questions posted in response to the aforementioned article. Therefore, the philosophy behind this article is to demystify pointers by using them in a simple working example.

Terminology and Technique

When working with pointers, I employ the following terminology and techniques:

  • Value - The actual value of the specified data type
  • Pointer To - A pointer to the memory location of a specified data type
  • Address Of - The physical memory address of a given data type
  • Reference To - A reference to a given data type, NOT a copy of it (C++ only)
  • Dereferenced Value - The value of the data type pointed to by a pointer

The subject of pointers deals heavily with the use of the symbols * and &, and to a beginner, this is often confusing; especially when different people put the modifers in different places (me and the VS6 class wizard for example :P ). My technique is to keep the modifiers next to what they are modifying. For example:

C++
// Initialize a variable, 'x', of type 'int' to the 'value' 5.
int x = 5;

// Initialize a variable, 'px' of type 'pointer to int' to the value of
// 'the address of' x.
int* px = &x;
----      --
|         |
|         |- This is read "address of" x
|
|- Notice I put the * modifier after int, not before px (int* px not
   int *px).  The compiler doesn't care, however I am modifying the
   data type, not the variable. Thus I remain consistent to my technique.

// Now initialize a variable 'ref_to_x' as a reference to x.
int& ref_to_x = x;
----
|
|- Again, I keep the modifier with what I am modifying.

// Initialize a variable,'deref_px' of type 'int' to the 'dereferenced
// value' of px.
int deref_px = *px;
               ---
               |
               |- Notice in this case, the * is with px, because that is
                  what I'm modifying.

Ok, with that out of the way, we continue on.

Passing By Reference

There have been many a question floating around about: passing values by reference. Why would you pass by reference? Passing a value by reference speeds things up and slims things down, because you are not making a copy of the variable passed. This may not seem too important when making a single function call in the whole life of your application's instance, but when calling a function say 1800 times in a minute; performance becomes a major motivating factor. Also, when modifying a copy of a value, the original value remains unchanged (which may be desirable in some cases), where when passing by reference, the original variable is modified. The downloadable example program will illustrate why, but here is a brief example that should clear up the confusion:

Consider the following functions:

C++
void MyFunctionByCopy(CString x)
{
   CString y = "Test by copy.";
   x.Format("%s",static_cast<const char*>(y));

   return;
}

void MyFunctionByReference(CString& x)
{
   CString y = "Test by reference.";
   x.Format("%s",static_cast<const char*>(y));

   return;
}

Note: The first one will create a copy of the variable passed, where the latter one will modify the variable itself. Note that the reference to int is an automatically dereferenced pointer, thus pointer notation is not required. Check the sample program for a working example of this in action.

Given the two functions above, can you guess the values of the results in the following example?

C++
{
    CString szTestOne = "Test Me One";
    CString szTestTwo = "Test Me Two";

    MyFunctionByCopy(szTestOne);
    MyFunctionByReference(szTestTwo);

    printf("\nszTestOne = %s", static_cast<const char*>(szTestOne));
    printf("\nszTestTwo = %s", static_cast<const char*>(szTestTwo));
}

For those who can't handle the suspense, szTestOne = "Test Me One" and szTestTwo = "Test by reference.". If you don't believe me, try it.

Pointer to a Pointer

Another lingering concern is the pointer to a pointer. If you subscribe to my technique, a pointer is nothing more than an arbitrary data type, and thus a 'pointer to a pointer' is nothing more than a plain old pointer. Sure, it may appear daunting and somewhat foreign, but break it down to its bare essentials and there's really nothing to it. So in fact, you can also have 'pointers to pointers to pointers' and 'pointers to pointers to pointers to poin...' well, you get the idea.

For anyone interested in what such an implementation may look like, consider the lines below. You may try this on your own and see the results.

C++
int   x   = 7;
int*  px  = &x;
int** ppx = &px;

printf("\nx     = %d", x    );
printf("\n*px   = %d", *px  );
printf("\n**ppx = %d", **ppx);

Why Use Pointers at All?

I can remember when I first came across pointers when beginning C++, and after much confusion, I wondered: "why use pointers at all?" I can just instantiate explicit instances of all my variables and not worry about it. (Notice I was all fancy using words like instantiate and explicit instance, yea I knew C++... right...) Well, with experience comes humility and wisdom; and with CodeProject comes enlightenment and encouragement. Venturing off into applications beyond the command line scripts (I say scripts, because my entire program was inside main() and I made no function calls. My idea of code reuse was cut-and-paste) I was coding before, I found myself in a whole new world.

As it turns out, in real-world programming, you don't always have the privilege of knowing how many things you will be analyzing, or how big your arrays need to be. You don't know how much system resources your user's machine will have. You begin to worry about things like optimization and passing by reference. You start using built-in functions that take pointers by default, although you may not know why...

In my personal experience (all two years of it), the primary driving forces behind my exodus into the realm of pointers are the wonderful operators:

new and delete

Although this is not a comprehensive guide to pointers, I feel you cannot talk about pointers without mentioning the aforementioned operators. Why are these operators so important? They are important not only because they allow you to dynamically allocate memory to and from your program, but they do it through the use of pointers. There is much information available on this topic so I will not go into vast detail, but I will cover the basic highlights. Again consider the following code:

C++
int* pNewInt = new int;
*pNewInt = 7;
printf("\nThe pointer, pNewInt, is located at memory address: 0x%X",
       &pNewInt);
printf("\npNewInt points to memory location 0x%X and contains the
       value %d.",
       pNewInt, *pNewInt);
delete pNewInt;

In this code, we created a new 'pointer to int' with the keyword new. Note that new returns a 'pointer to int'. An important thing to remember is to delete your new pointers after you are done with them. If you don't, you will have many a memory leak and risk utilizing all resources of the target machine. The details of this can be found elsewhere, but the point remains: new is a powerful friend and is worth the time to understand.

In Conclusion

Pointers may seem intimidating at first, but they are in fact a fundamental concept in C and C++. Reading about them will only take you so far; it is the experience in using them and the enlightenment of harnessing their power that will ultimately contribute to making the world a better place. So enjoy your pointers. Use them often, and take good care of them. But most importantly, use them with courage and confidence and you will be greatly rewarded when you pass beyond that great exception in the sky...(Mark Conger - Death of a Coffee Pot)

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
CEO Aspen Insights
United States United States
Walter Storm is currently doing quantitative research and data science. Originally from Tunkhannock, PA., he has a B.S. in Aerospace Engineering from Embry-Riddle Aeronautical University[^], and an M.S. in Systems Engineering from SMU[^]. He has been professionally developing software in some form or another since January of 2001.

View Walter Storm's profile on LinkedIn.[^]

Comments and Discussions

 
QuestionThis is why use pointers Pin
steveb4-Jan-20 9:27
mvesteveb4-Jan-20 9:27 
Generalpointing to a memory location Pin
SwimmerDave15-Feb-06 17:35
SwimmerDave15-Feb-06 17:35 
Was just wondering if there was an easy way to point to an address in memory held by a different program,
and to read the value that was located there?
For example I was trying to create a program which could read the time displayed in the program Mine Sweeper and print it out.
Thanks
GeneralRe: pointing to a memory location Pin
Nitron16-Feb-06 2:44
Nitron16-Feb-06 2:44 
Generalpointers Pin
Member 11530705-Jun-05 17:14
Member 11530705-Jun-05 17:14 
GeneralRe: pointers Pin
Christian Graus5-Jun-05 18:39
protectorChristian Graus5-Jun-05 18:39 
GeneralRe: pointers Pin
cristitomi15-Feb-07 4:26
cristitomi15-Feb-07 4:26 
GeneralRe: pointers Pin
cristitomi15-Feb-07 4:31
cristitomi15-Feb-07 4:31 
Generalform Pin
Oriocat29-Jul-04 9:36
Oriocat29-Jul-04 9:36 
GeneralProblem in RiechEdit..!! Pin
boonshajayan9-Aug-03 1:16
boonshajayan9-Aug-03 1:16 
GeneralRe: Problem in RiechEdit..!! Pin
Nitron9-Aug-03 4:13
Nitron9-Aug-03 4:13 
Generalplacement of * Pin
ErikHaugen30-Jul-03 16:25
ErikHaugen30-Jul-03 16:25 
GeneralRe: placement of * Pin
John M. Drescher30-Jul-03 16:37
John M. Drescher30-Jul-03 16:37 
GeneralA reason why Pin
dog_spawn2-Aug-03 5:57
dog_spawn2-Aug-03 5:57 
GeneralRe: A reason why Pin
Bamaco223-Dec-03 11:25
Bamaco223-Dec-03 11:25 
GeneralRe: A reason why Pin
sofus4-Apr-06 3:27
sofus4-Apr-06 3:27 
QuestionWhat is the difference? Pin
User 665828-Jul-03 8:46
User 665828-Jul-03 8:46 
AnswerRe: What is the difference? Pin
Nitron28-Jul-03 11:06
Nitron28-Jul-03 11:06 
GeneralRe: What is the difference? Pin
Johnny ²28-Jul-03 21:33
Johnny ²28-Jul-03 21:33 
GeneralRe: What is the difference? Pin
User 665829-Jul-03 0:50
User 665829-Jul-03 0:50 
GeneralRe: What is the difference? Pin
Johnny ²29-Jul-03 4:21
Johnny ²29-Jul-03 4:21 
GeneralRe: What is the difference? Pin
User 665829-Jul-03 6:18
User 665829-Jul-03 6:18 
GeneralRe: What is the difference? Pin
Nitron29-Jul-03 6:48
Nitron29-Jul-03 6:48 
GeneralRe: What is the difference? Pin
RabidCow31-Jul-03 11:40
RabidCow31-Jul-03 11:40 
AnswerRe: What is the difference? Pin
PerFnurt30-Jul-03 11:47
PerFnurt30-Jul-03 11:47 
AnswerRe: What is the difference? Pin
Tim Smith1-Oct-03 7:51
Tim Smith1-Oct-03 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.