Pointers: A Better Understanding

Issues 1..9
Web & XML

1.   Introduction
2.   What is a pointer?
3.   Declaring Pointers
4.   Using Pointers
5.   The Address Operator
6.   Pointers to Arrays
7.   Pointers to Pointers
8.   Conclusion

by Moe Elzubeir
last updated 2000/01/31 (version 1.1)
also available as XML

IRC: kuru @EFnet's #c
Email: moe@bigger.com


Many beginners have a hard time grasping the concept of pointers. And even the some-what intermediate programmers face some problems with pointers. Since it seems to be the biggest obstacle for all, I thought maybe a little explanation of how it all works would suit the needs.

What is a pointer?

In the simplest of words, a pointer is a variable that holds the memory address of another variable. Think of it as the mailing address you put on an envelope. That address points to the house, or po box of the person you want to send the letter to. The pointer simply holds the address. The same way the envelope holds the address of the person. It points to the person.

Get it? Pretty simple concept.

Now why we find this a convenient way of doing things is another matter that is best discovered as you go along discovering the "ways of pointers". ;)

Declaring Pointers

Declaring a pointer is very simple: char *mypntr;.

In this case the pointer points to the memory address of a char variable. Note that it should point to a char variable and not a float or any other variable. The compiler might start groaning and producing strange sounds if you don't do it right. ;)

Using Pointers

Now let's do a little exchange. We will exchange the contents of two variables using pointers.

int myvar1 = 13, myvar2 = 69, temp;
int *mypntr;

mypntr = &myvar1; /* In case you don't know the &myvar1 means the address of
                     which the contents of myvar1 are stored and not the
                     actual value of myvar1; & is called the address
                     operator */
temp = *mypntr;   /* copies the value of the variable that mypntr is
                     pointing to into temp, in which case myvar1 */

*mypntr = myvar2; /* copies the contents of myvar2 into the address pointed
                     to by myprntr */

myvar2 = temp;    /* copies temp into myvar2 - simple */

We have just exchanged the values using pointers. This is pretty useless it seems, but it's a good exercise to get a grip on the concepts.

The Address Operator

I think it's time for a little light turned on to the address operator. There are a few things you should know about it.

There are a few things you cannot do with the address operator. Some may seem obvious, but are worth pointing out.

1. var_address = &13;
You cannot use the address operator with constants.

2. var_address = &(value + 69);
You cannot use the address operator with expressions involving operators such as + and / given the definition that value=9 (for example).

3. var_address = ®
Cannot use the address operator by preceding register variables given the definition register reg.

Pointers to Arrays

Pointers and arrays are closely related and thus confused together. Let's consider this segment of code:

float salary[50];
float *float_ptr;

Now lets consider this:

float_ptr = salary;
float_ptr = &salary[0];

Well, these two statements are the same thing. They do the exact same thing, which is have float_ptr point to the first element of the salary array.

A string constant (e.g., "C Scene") is stored as an array of chars with a null terminator added as the last char. But, look at this:

char *char_ptr = "C Scene";

Not only does it define the pointer but also initializes it to the address of the first character in the string. The same statement could have been written like:

char *char_ptr;
char_ptr = "C Scene";

Pointers to Pointers

Although it might strike you funny, but this will become a daily thing for you if you start doing any coding. Yes, it is a pointer that points to a pointer that points the address of a certain variable.

A good example would be the command line arguments vector argv in the prototype of int main(int argc, char **argv);. The char **argv, which is also sometimes written char *argv[], is a pointer to a pointer.


There is a lot more to do with pointers. But I am truely tired and exhausted. And will finish off some other issue. I have only covered the "basics" of it all.

Moe Elzubeir

This article is Copyright © 1997 by C-Scene. All Rights Reserved.

[Back to top] Copyright © 1997-2000 by C-Scene. All Rights Reserved.

Part of the graphics and stylesheets used to generate this site are
Copyright © 1999-2000 by Apache Software Foundation.