An example of pointer arithmetic

This is an example C program demonstrating pointer arithmetic.

#include <stdio.h>

int main ()
{
    int x;
    int * x_p;

    x_p = x;
    printf ("%p\n", x_p);
    x_p = x_p + 1;
    printf ("%p\n", x_p);
    return 0;
}

Download it here.

The output of the example looks like this:

0xbfbfea78
0xbfbfea7c

Notice here that the second value is four bytes beyond the first value.

Ask and answer questions on C in the new C forum

Copyright © Ben Bullock 2009-2012. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (ben.bullock@lemoda.net) / Privacy / Disclaimer