C Pointers

Introduction

Pointers are variables that contain memory addresses as their values. A variable name directly references a
value and a pointer indirectly references a value.

Example 1

int *ptr;

Declares the variable ptr to be of type int * , i.e a pointer to an integer value.

Pointers should be initialized to prevent unexpected results.

Pointer Operators

The &, or address operator, is a unary operator that returns the address of its operand.
The * operator, commonly referred to as the indirection operator or dereferencing
operator, returns the value of the object to which its operand (i.e., a pointer) points.

Example 2
In the following example memory could be visualized as:
5000 [10]

6000[5000]

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
 
    int x = 10;
    int *ptr;
    ptr = &x;
 
    printf("x = %d\n",x);
    printf("*ptr = %d\n",*ptr);
 
  return 0;
}

Example 3

The printf conversion specification %p outputs the memory location as a hexadecimal integer. Notice that the address of a and the value of aPtr are identical in the output, thus confirming that the address of a is indeed
assigned to the pointer variable aPtr.

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
    int a; /* a is an integer */
    int *aPtr; /* aPtr is a pointer to an integer */
 
    a = 7;
    aPtr = &a; /* aPtr set to address of a */
 
    printf( "The address of a is %p"
    "\nThe value of aPtr is %p", &a, aPtr );
 
     printf( "\n\nThe value of a is %d"
    "\nThe value of *aPtr is %d", a, *aPtr );
 
    printf( "\n\nShowing that * and & are inverses of "
    "each other.\n&*aPtr = %p"
    "\n*&aPtr = %p\n", &*aPtr, *&aPtr );
 
  getchar();
  return 0;
}

Calling Functions by Reference

There are two ways to pass arguments to a function, call by value and call by reference.All function calls in C are call by value. The return statement may be used to return one value from a called function to a caller. Many functions require the capability to modify one or more variables in the caller, or to pass a pointer to a large data object to avoid the overhead
of passing the object call by value (which, of course, requires making a copy of the object). For these purposes, C provides the capabilities for simulating call by reference. When calling a function with arguments that should be modified, the addresses of the
arguments are passed. This is normally accomplished by applying the address operator (&) to the variable whose value will be modified.

Example 4
The following example present two versions of a function that cubes an integer— cubeByValue and cubeByReference.

#include <stdio.h>
#include <stdlib.h>
 
int cubeByValue( int );
void cubeByReference(int *);
 
int main(int argc, char *argv[])
{
    int number1 = 10;
 
    printf( "The original value of number is %d", number1 );
    number1 = cubeByValue( number1 );
    printf( "\nThe new value of number is %d\n", number1 );
 
    int number2 = 5;
 
    printf( "The original value of number is %d", number2 );
    cubeByReference( &number2 );
    printf( "\nThe new value of number is %d\n", number2 );
 
  getchar();
  return 0;
}
 
int cubeByValue(int x)
{
    return x*x*x;
}
 
void cubeByReference(int *ptr)
{
    *ptr = (*ptr) * (*ptr)* (*ptr);
}

The following figures analyze the above program:

Slide1.GIF
Slide2.GIF
Slide3.GIF
Slide4.GIF
Slide5.GIF
Slide6.GIF
Slide7.GIF
Slide8.GIF

Using the const Qualifier with Pointers

The const qualifier enables the programmer to inform the compiler that the value of a particular variable should not be modified.
If a value does not (or should not) change in the body of a function to which it is passed, the value should be declared const to ensure that it is not accidentally modified.

Pointer Expressions and Pointer Arithmetic

Pointers are valid operands in arithmetic expressions, assignment expressions, and comparison expressions. However, not all the operators normally used in these expressions are valid in conjunction with pointer variables.

Example 5

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
    int array[5] = {1,2,3,4,5};
 
    int *ptr = 0;
    ptr = &array;
 
    printf("%d\n",*ptr);
    ptr++;
    printf("%d\n",*ptr);
 
    getchar();
    return 0;
}

The Relationship between Pointers and Arrays

Arrays and pointers are intimately related in C and may be used almost interchangeably. An array name can be thought of as a constant pointer. Pointers can be used to do any operation involving array subscripting.

Performance Tip: Array subscripting notation is converted to pointer notation during compilation, so writing array subscripting expressions with pointer notation can save compile time.

Example 6

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
 
    int b[] = { 10, 20, 30, 40 };
    int *bPtr = b; /* set bPtr to point to array b */
    int i, offset;
 
    printf( "Array b printed with:\n"
    "Array subscript notation\n" );
 
    for ( i = 0; i < 4; i++ )
        printf( "b[ %d ] = %d\n", i, b[ i ] );
 
    printf( "\nPointer/offset notation where\n"
    "the pointer is the array name\n" );
 
     for ( offset = 0; offset < 4; offset++ )
        printf( "*( b + %d ) = %d\n", offset, *( b + offset ) );
 
   printf( "\nPointer subscript notation\n" );
 
     for ( i = 0; i < 4; i++ )
       printf( " bPtr[ %d ] = %d\n", i, bPtr[ i ] );
 
     printf( "\nPointer/offset notation\n" );
 
     for ( offset = 0; offset < 4; offset++ )
        printf( "*( bPtr + %d ) = %d\n", offset, *( bPtr + offset ) );
 
    getchar();
    return 0;
}

Pointers to Functions

A pointer to a function contains the address of the function in memory. A function name is really the starting address in memory of the code that performs the function’s task. Pointers to functions can be passed to functions, returned from functions, stored in arrays, and assigned to other function pointers.

Examples

Data Structures

More in Data Structures section.

Implementing Call by Reference

Passing arrays as arguments

Referring to Functions

Alias to another value

Representing character strings

Direct access to system memory

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.