Manipulate Pointers in Functions, Part V

In Part II of this series, I showed code that let you pass a pointer’s address to a function. Within that function, the address can be manipulated and value at that address displayed. Can the same thing be done with a two-dimensional array, which is loosely related to a ** pointer?

As a reference, here is the fourchar() function I used in Part II’s sample code:

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

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

For a second, forget pointers and pointers-to-pointers. Consider instead an array.

A single dimension array is like a pointer’s cousin. Lots of C programmers rely on the similarity to avoid using pointers. In a way, a two-dimensional array is like a pointer-pointer. The problem with this similarity is that it doesn’t really translate well.

For example, assume that you have a two-dimensional array, one that stores two strings:

char text[2][5] = { "ABCD", "WXYZ" };

You want to send strings from this array to the fourchar() function. So you write the following function:

void fourchar(char p[][])
{
    int x;

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

This code won’t compile. The compiler has no idea what the argument p[][] is. Yet, your code needs to manipulate a char array in the manner shown above. The function can take p[] as an argument, but within the function, how can you access individual array elements? One solution is to use a pointer-pointer instead.

The following code is similar to last week’s Lesson code, but uses pointers instead of array notation.

#include <stdio.h>

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

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

int main()
{
    char *text[] = { "ABCD", "WXYZ" };

    fourchar(&text[0]);

    return(0);
}

In the main() function, *text[] is an array. You could declare it as a two-dimensional array, but then you lock yourself out of the option of sending a text[][] item to a function. So you must declare an array of char pointers to make the function call work. (You could also declare the variable as **text, but that would involve using malloc() and assigning strings and other nonsense.)

At Line 19, the fourchar() function is called with the address of the first string in the *text[] array.

In the fourchar() function, variable **p represents the address of a pointer, which is the address of the string. A for loop marches through each character in the string. Variable c holds the character fetched. It’s not a required variable, but it does eliminate a layer of parentheses that would otherwise cluster inside the putchar() function. Here’s how Line 10 unwinds:

c = *(*p+x);

At the core, *p represents an address, the address of string "ABCD". Variable x is an offset from that address. You must format it as *p+x, which evaluates as an address. To fetch the character at that address, you enclose *p+x in parentheses and use the asterisk: *(*p+x).

In the end, the code marches through each character in the array, just as if the p[][] format were used.

If you like, you can add a second statement to the main() function:

fourchar(&text[1]);

This statement processes the second string in the *text[] array.

Leave a Reply