TOC | Prev | Next

Pointers

A pointer is something that points to a piece of data, or sometimes to a function.

Take the address of something with &, and dereference with *.

pointer-intro.c

#include <stdio.h>

#define MAX_NAME 40

int main( void ) {
    char name[MAX_NAME + 1] = "Andy";
    char *p = name;

    printf( "First letter  at %p = %c\n", p, *p );

    p = &name[1];
    printf( "Second letter at %p = %c\n", p, *p );

    p++;
    printf( "Third letter  at %p = %c\n", p, *p );

    printf( "Fourth letter at %p = %c\n", p+1, *(p+1) );

    /* *(p+1), not *p+1, because that's (*p)+1 */

    return 0;
}

$ pointer-intro

First letter  at 0xbffff573 = A
Second letter at 0xbffff574 = n
Third letter  at 0xbffff575 = d
Fourth letter at 0xbffff576 = y
TOC | Prev | Next