Manipulate Pointers in Functions, Part III

I know C programmers who make it their goal to avoid pointers — even where necessary. It’s possible to do so, and many have cheat sheets to help them. Then comes the ** pointer monster, which can be avoided altogether . . . until you need to manipulate a pointer variable’s own address within a function. Then you’re cursed.

Like every variable type, a pointer has an address in memory, a location. So when you create char pointer *text, the program requests that a pointer-sized chunk of memory be set aside. When that pointer is initialized, the address of the variable it references is stored in its chunk-o-memory.

From the sample program from last week’s Lesson, variable *text is inialized to a string buffer in the main() function:

char *text = "ABCD\n";

When variable text is used, it refers to the starting location, the memory address, where "ABCD\n" is located.

When variable *text is used, it references the character (because text is a char pointer) at the memory address stored in text, which would be the character 'A'. Figure 1 illustrates this concept.

Figure 1. The pointer variable text contains an address; variable *text references the contents of that address.

To manipulate the contents of the pointer’s address in a function, you just pass the pointer. That’s what I showed in Part I of this series. But I also demonstrated that you can’t manipulate the pointer’s address; it’s unaffected because the function doesn’t own the pointer variable.

When you need to manipulate a pointer’s address in a function, pass a pointer to the pointer.

Bear with me.

You start by getting the address of the pointer variable: Use the & operator just as you would on any other variable. Figure 2 illustrates.

Figure 2. The memory location of variable text, which holds the address of string "ABCD\n".

In Figure 2, you see the address of pointer text, which is shown as value 0x7FE88. The contents of that location are the address of the string "ABCD\n", or 0x7FF00, which is illustrated in Figure 1.

The pointer’s address by itself is a curiosity, but the value can be saved in a pointer-pointer variable. The char variable **p is designed to hold the address of a pointer, or the address of (*) the address of (*) some variable.

This is spot where programmers become overwhelmed, which is easy to do to. References to references are something that human brains don’t regularly deal with.

To make the &text address value useful, last week’s program created the **p variable. It’s assigned to the value of &text when that value is passed to the fourchar() function. As a statement, the assignment looks like this:

p = &text;

Figure 3 illustrates all the gears and cogs of this machination.

Figure 3. How the *text and **p variables work together.

The goal of all these words and illustrations is that if you can’t get your head wrapped around the pointer-pointer concept, at least you can look up a handy reference that explains how the various pieces work. That’s what I do in Figure 3, which is often what I write down while I’m programming so that I can use the proper references.

And now the Big Reveal: The pointer and pointer-pointer relationship has a lot to do with two-dimensional arrays. I cover this topic in next week’s Lesson.

Leave a Reply