Your browser doesn't support JavaScript Advanced Pointers – Windows Programming

Advanced Pointers

Pointers to Pointers (Multiple Indirection)

A normal pointer contains the address of a variable. When defining a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location of the variable.  A pointer to a pointer is declared by placing an additional asterisk in front of the variable name. For example, the following declaration declares a pointer to a pointer of type int 

int **var;

Accessing that value pointed to in the above direct is by applying the asterisk operator twice, as is shown below

#include <stdio.h>

int main(void)
{
    int value = 25;

    int *ptr = &value;     // Pointer to value
    int **ptr2 = &ptr;     // Pointer to ptr

    printf("Value: %d\n", value);
    printf("Using ptr: %d\n", *ptr);
    printf("Using ptr2: %d\n", **ptr2);

    return 0;
}

Output:

Value: 25
Using ptr: 25
Using ptr2: 25

In this example:

  • value stores the integer 25.
  • ptr stores the address of value.
  • ptr2 stores the address of ptr.
  • *ptr accesses the value stored in value.
  • **ptr2 follows both pointers to access the value stored in value.

Pointers to pointers are useful in situations such as dynamically allocated multidimensional data, and when a function needs to modify a pointer passed to it.

Pointers and Multidimensional Arrays

A two-dimensional array stores its elements in contiguous memory locations, with each row following the previous row. A pointer can be used to access the individual elements of the array.

To point to the first integer in a two-dimensional array named num, you can write:

int *ptr = &num[0][0];

Here, ptr points to the first element of the array. Incrementing the pointer by one moves it to the next integer element in memory. This allows you to traverse the array sequentially using pointer arithmetic.

The following program prints all the elements of a two-dimensional array using a pointer and a for loop.

#include <stdio.h>

int main(void)
{
    // Declare and initialise a two-dimensional array
    int value[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    // Point to the first element
    int *ptr = &value[0][0];

    int total_cells = 12;

    // Print all elements using pointer arithmetic
    for (int i = 0; i < total_cells; i++) {
        printf("%d ", *(ptr + i));
    }

    return 0;
}

}

Output:

1 2 3 4 5 6 7 8 9 10 11 12

In this example:

  • value[3][4] declares an array with three rows and four columns, containing 12 integers.
  • &value[0][0] obtains the address of the first element.
  • ptr + i calculates the address of an element relative to ptr.
  • *(ptr + i) dereferences that address to retrieve the element’s value.

Important: Although the array’s elements are stored contiguously, strictly conforming C code should not use pointer arithmetic to move beyond the first row’s array boundary. For a fully portable way to traverse the entire two-dimensional array, use nested loops:

for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 4; col++) {
        printf("%d ", value[row][col]);
    }
}

This approach explicitly accesses each element within its row and column bounds.

Array of pointers

An array of pointers is an array that holds memory locations. The following statement declares an array of 5 pointers to type char –

char *str[5];

Each element of the str array acts as an individual pointer to type char. These declarations can be combined with initialisation statements as below

char *str[5] = { “one”, “two”, “three” };

This code declares an array of 5 pointers to type char and initialises them to point to 5 strings. It then uses a for loop to display each element.

# include <stdio.h>
int main( void )
{
char *str[5] = { "One", "Two", "Three", "Four","five"};
int count;
for (count = 0; count < 5; count++)
printf("%s ", str[count]);
printf("\n");
return 0;
}

Output:
One Two Three Four Five

In this example:

  • char *str[5] declares an array containing five pointers to characters.
  • Each pointer refers to the first character of a string literal.
  • str[count] accesses an individual pointer in the array.
  • %s tells printf() to display the string referenced by that pointer.

Note: String literals should not be modified. The declaration char *str[5] allows the pointers to be changed, but it does not make the string literals themselves modifiable.

Pointers to Functions

A pointer to a function stores the address of a function, allowing the function to be called indirectly through the pointer. As with other pointers in C, a function pointer must be declared before it is used.

The general form of a function pointer declaration is:

type (*function_ptr)(parameter_list);

Coded examples of how to declare some function pointers are listed below

int (*function)(int x);
char (*function)(char *p[]);
void (*functions)();

Parentheses are used around the pointer name to override operator precedence. Without the parenthesis, the function declaration will not be evaluated in the correct order.

Function pointers are useful for implementing callback mechanisms, passing functions as arguments to other functions, and selecting which function to call at runtime. They can also be stored in arrays to provide a convenient way to select between different functions.

The following short program demonstrates how to declare and call a function pointer

#include <stdio.h>
#include <string.h>

void function(char arrayvalue[30]);  // Function declaration

void (*ptrfunc)(char arrayvalue[30]); // Function pointer declaration

int main(void)
{
    char arrayvalue[30] = "outside array";

    ptrfunc = function;  // Assign the function address to the pointer

    printf("%s\n", arrayvalue);

    ptrfunc(arrayvalue); // Call the function through the pointer

    printf("%s\n", arrayvalue);

    return 0;
}

void function(char arrayvalue[30])
{
    strcpy(arrayvalue, "function return");
}

Output:

outside array
function return

In this example:

  • void function(char arrayvalue[30]) declares a function that accepts a character array and returns no value.
  • void (*ptrfunc)(char arrayvalue[30]) declares a pointer to a function with the same parameter and return types.
  • ptrfunc = function; assigns the function’s address to the pointer.
  • ptrfunc(arrayvalue); calls the function through the pointer.
  • strcpy() replaces the contents of arrayvalue with “function return”. Because the array is passed to the function, the changes are visible in main().

Note: In a function parameter declaration, char arrayvalue[30] is adjusted to char *arrayvalue. The array size does not form part of the function pointer’s type. The example includes <string.h> because strcpy() is declared in that header.