Basics of Pointers in C Programming - dummies

By Dan Gookin

Memorize this sentence: A pointer is a variable that contains a memory location. You must accept the insanity of the pointer before moving on. True, though you can get at a variable’s memory location, or address, by using the & operator, the pointer is a far more powerful beast.

A pointer is a type of variable. Like other variables, it must be declared in the code. Further, it must be initialized before it’s used. That last part is really important, but first the declaration has this format:

type *name;

As when you declare any variable, the type identifies the pointer as a char, int, float, and so on. The name is the pointer variable’s name, which must be unique, just like any other variable name. The asterisk identifies the variable as a pointer, not as a regular variable.

The following line declares a char pointer, sidekick:

char *sidekick;

And this line creates a double pointer:

double *rainbow;

To initialize a pointer, you must set its value, just like any other variable. The big difference is that a pointer is initialized to the memory location. That location isn’t a random spot in memory, but rather the address of another variable within the program. For example:

sidekick = &lead;

The preceding statement initializes the sidekick variable to the address of the lead variable. Both variables are char types; if not, the compiler would growl like a wet cat. After that statement is executed, the sidekick pointer contains the address of the lead variable.

If you’re reading this text and just nodding your head without understanding anything, good! That means it’s time for an example.

The source code in An Example includes comments to help describe two crucial lines. The program really doesn’t do anything other than prove that the pointer sidekick contains the address, or memory location, of variable lead.


#include <stdio.h>
int main()
 char lead;
 char *sidekick;
 lead = 'A'; /* initialize char variable */
 sidekick = &lead; /* initialize pointer IMPORTANT! */
 printf("About variable 'lead':n");
 printf("And variable 'sidekick':n");

Other things to note: Line 12 uses two tab escape sequences to line up the output. Also, don’t forget the & in Line 14, which fetches the variable’s address.

Exercise 1: Type the source code from An Example into your editor. Build and run.

Here’s the output:

About variable 'lead':
Size 1
Contents A
Location 0x7fff5fbff8ff
And variable 'sidekick':
Contents 0x7fff5fbff8ff

The addresses (in the example) are unique for each system, but the key thing to note is that the contents of pointer sidekick are equal to the memory location of variable lead. That’s because of the assigning, or initialization, that takes place on Line 9 in the code.

It would be pointless for a pointer to merely contain a memory address. The pointer can also peek into that address and determine the value that’s stored there. To make that happen, the * operator is prefixed to the pointer’s variable name.

Exercise 2: Modify your source code from Exercise 1 by adding the following statement after Line 16:

printf("Peek valuet%cn",*sidekick);

Build and run. Here’s the output:

About variable 'lead':
Size 1
Contents A
Location 0x7fff5fbff8ff
And variable 'sidekick':
Contents 0x7fff5fbff8ff
Peek value A

When you specify the * (asterisk) before an initialized pointer variable’s name, the results are the contents of the address. The value is interpreted based on the type of pointer. In this example, *sidekick represents the char value stored at a memory location kept in the sidekick variable, which is really the same as the memory location variable lead.

To put it another way:

  • A pointer variable contains a memory location.

  • The *pointer variable peeks into the value stored at that memory location.