Manipulate Pointers in Functions, Part II

When a function must manipulate a pointer’s address, the argument passed is a pointer-pointer, not a pointer. Confused? Oh, I’m just getting started . . .

As a review:

  • A pointer is a variable that holds a memory address of another variable.
  • The pointer variable can be used to manipulate the contents of the address.
  • The pointer variable can be used to manipulate the address (the address value can be changed).

Manipulating the contents of a pointer in a function is easy. To manipulate the pointer’s address, however, you must pass the pointer’s address, or a pointer-pointer, to the function.

It’s true: Like every other variable type, pointers have addresses in memory.

In last week’s Lesson, the pointer text is passed to function fourchar(). Here’s the declaration of that function:

void fourchar(char *p)

And this statement calls the function from main():

fourchar(text);

Within the function, you use pointer p to manipulate values at memory location text. You can change pointer p in the function, but the original pointer text dwells in the main() function unchanged — that is, unless you pass its address to the function. To do so, you use this statement in main():

fourchar(&text);

The address of text can be manipulated in function fourchar(). The declaration of fourchar() must be changed to accept the new variable type:

void fourchar(char **p)

Yes, it’s the ugly ** operator that drives everyone nuts, but that’s how you can modify a pointer’s address — as well as the contents at that address — within a function. Indeed, things get truly ugly in this operation, but they’re still workable.

The following modification to last week’s code accepts a pointer-to-a-pointer as an argument to the fourchar() function, manipulates the pointer’s address directly, as well as displays the contents of the pointer-to-a-pointer address:

#include <stdio.h>

void fourchar(char **p)
{
    int x;

    for(x=0;x<4;x++)
    {
        putchar(**p);
        (*p)++;
    }
}

int main()
{
    char *text = "ABCD\n";

    fourchar(&text);
    putchar(*text);

    return(0);
}

Within function fourchar(), the character at *text is referenced as **p. This is where some programmers doze off, but pay attention! **p translates as, “The contents of the address stored at address p.”

Within the fourchar() function, variable *p is a memory location, not a character. It holds the value passed to the function, the address of pointer text. To access that memory location’s value, what would be *text in the main() function, you use **p in function fourchar().

The next line manipulates the address stored in *p — the value of the main() function’s text pointer variable:

(*p)++;

In the main() function, you’d use text++, but because the address of text was passed to the fourchar() function, you need to use a pointer, *p. Further, because of the order of precedence, you must ensure that the pointer at *p is incremented, so you need parentheses: (*p)++.

Here’s sample output:

ABCD

Unlike last week’s code, the pointer’s address is changed within the fourchar() function. When the function returns, the address stored in text references the newline character, which is displayed in the output.

I’ll explain the pointer-to-pointer relationship in further detail, along with friendly illustrations, in next week’s Lesson.

Leave a Reply