C Advanced Pointers
Home | API | MFC | C++ | C | Previous | Next
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) {
// declare and populate 2d array
int value[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int *ptr = &value[0][0]; // set pointer ptr to array value
int total_cells = 12, i;
// output elements of the array value by using pointer ptr
for (i = 0; i < total_cells; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}
To create a pointer to a multidimensional array assign the address of the first element of the array to use the pointer address as below
int *ptr = &num[0][0];
The multi-dimensional array num will be saved as a continuous block in memory. To move between each element the value of ptr will need to be incremented by one.
In the following code segments, the num array contents are printed by using a for loop and incrementing the value of ptr.
#include <stdio.h>
int main(void) {
// declare and populate 2d array
int value[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int *ptr = &value[0][0]; // set pointer ptr to array value
int total_cells = 12, i;
// output elements of the array value by using pointer ptr
for (i = 0; i < total_cells; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}
An array of pointersis 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;
}
A pointer to a function points to the address of the executable code of the function.As with all C variables, it is necessary to declare a pointer to a function before using it. The general form of the declaration is as follows:
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 pointersare usefulfor implementing callback mechanisms and passing the address of afunction to another function. They are also useful for storing an array of functionsdynamically.
The following short program demonstrates how to declare and call a function pointer
#include <stdio.h>
void function(char arrayvalue[30]);/*function declaration*/
void (*ptrfunc)(char arrayvalue[30]);/* The pointer declaration. */
int main( void )
{
ptrfunc = function;/* Initialise ptrfuch to address of function. */
char arrayvalue[30]="outside array";
printf(" %s\n", arrayvalue);
ptrfunc(arrayvalue);//call function use *ptrfunc
printf(" %s\n", arrayvalue);
return 0;
}
void function(char arrayvalue[30])
{
strcpy(arrayvalue, "function return");/*change value of arrayvalue*/
}
Last Updated: 15 September 2022