int *p_int ;
The standard convention used in this book is to append the variable being pointed to with "p_" to designate its status
as a pointer. This is helpful when there are multiple variables of the same apparent type in a function, where some
are pointers and some are not. In order to access the information pointed to by a pointer, a programmer must use the
dereference operator (also the asterisk) to denote the reference.
If a variable is not a pointer, but needs to be modified inside a called function, then the address-of operator (&) can
be used to create an "on-the-fly" pointer that is passed to the function. Many programmers understand that the
scanf() function needs the address-of operator in front of the variables read into, but not all of them know why. It
is because scanf() needs to affect the variable where the information is being stored.
Pointers are usually used for keeping track of dynamically allocated memory. In this demonstration, the pointer is
redundant, but it will illustrate the principles of pointers.
#include <stdio.h>
int i ;
int *p_i = &i ;
i = 5 ;
printf( "%d " , i ) ;
*p_i = 7 ;
printf( "%d\n" , *p_i ) ;
return 0 ;
First, an integer i is created as a static variable of main(). Then, the pointer p_i is created and its value is set to
the address of i. Next, i is assigned the value of 5 and printed. Finally, the integer that p_i points to is assigned
the value of 7 and printed.
Polymorphic Data Structures in C/Pointers 2
Notice the use of dereference and address-of operators. The proper use of pointer, dereference and address-of
operators is key in an understanding of polymorphism.
char *p_char ;
p_char = (char *)malloc( sizeof ( char ) ) ;
More complexly, dynamic memory allocation is usually used when the amount of space to be reserved is not known
at the time of compilation. In the following program, the user is asked how many integers they would like to store,
and stores them.
#include <stdio.h>
#include <stdlib.h>
return 0 ;
Polymorphic Data Structures in C/Pointers 3
Notice how stdlib was included in the header. malloc() is declared in stdlib.h, so it has to be included
along with stdio.h in programs that use it.
Notice also how the space reserved by malloc() is referred to using the standard syntax for arrays. This is
because malloc() reserves space sequentially (all in a row), so pointer arithmetic (discussed below) can be
applied to it. In other words, when malloc() reserves space for more than one of a given type, it allocates space
for an array of that type.
The bottom two statements perform exactly the same operations. Because we will be working with pointers to
structures for the majority of this book, the dereference-member operator will be used more often. Note that
sub-members must still be referred to using the normal member operator.
Pointer Arithmetic
Performing pointer arithmetic is a very simple concept, but a somewhat difficult practice. Performing pointer
addition on arrays is done implicitly, with the subscript operators []. The following statement would return TRUE:
int A[4] ;
( A[2] == *(A + 2) ) ;
The addition to a pointer causes the reference address to be "moved forward" by the specified number of units,
multiplied by the size of the type. So, &A[2] is the location of A[0], plus the size of two integers in memory (thus
giving you the third item in the array, since arrays start indexing at 0). Pointer arithmetic can be applied to unions
and structures as well, but this method is not used as often in favor of member referencing.
Polymorphic Data Structures in C/Pointers 4
Pointers to Functions
A very powerful feature of the C language is the ability to take the address of a function and store it as a pointer. In
this manner, one could theoretically write a function, then have that function call a different function that is known
only at runtime (decreasing the amount of conditional programming needed). Every C programmer is familiar with
the standard function declaration syntax:
Similar to variables, functions are also typed. Assuming they are not of type void, all functions return data, and that
data must have type. As a side effect of this structure, a programmer can declare pointers to functions.
This code can be used to execute any number of functions in the same line of code, demonstrated below in this text
manipulation program.
#include <stdio.h>
char input_string[32] ;
int state , i ;
void (*p_function)( char *string ) ;
printf( "Would you like to print this string in reverse? 1 for yes,
0 for no. " ) ;
scanf( "%d" , &state ) ;
if ( state == 1 ) {
p_function = print_reverse ;
} else if ( state == 0 ) {
p_function = print_normal ;
} else
printf( "Error: Must enter 0 or 1. Exiting." ) ;
p_function( input_string ) ;
return 0 ;
}
Polymorphic Data Structures in C/Pointers 5
int i ;
printf( "\n" ) ;
}
void print_normal( char *string ) {
int i = 0 ;
printf( "\n" ) ;
In this program, there are two functions, print_reverse() and print_normal(). One simply prints the
string, and the other prints the string's contents out in reverse order. But the actual function call inside main() is
the same in both cases:
p_function( input_string ) ;
This is made possible through C's ability to reference functions by pointers to that function, and to properly pass
parameters to that function.
Article Sources and Contributors 6
License
Creative Commons Attribution-Share Alike 3.0 Unported
http:/ / creativecommons. org/ licenses/ by-sa/ 3. 0/