1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Clarification with pointer arithmetics eg *ip++ = 0

Discussion in 'C' started by briff, Nov 16, 2008.

  1. briff

    briff New Member


    if i have a code
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
        int a=10, *ip;
        ip =&a;
        printf("ip is %d and a is %d\n",ip,a);
        [B]*ip++ = 0;[/B]
        return 0;
    what do the code in bold do?

  2. briff

    briff New Member

    sorry the quote i bolded it is actually this:

  3. shabbir

    shabbir Administrator Staff Member

    So whats your query?
  4. xpi0t0s

    xpi0t0s Mentor

    It's undefined, because ip doesn't point to anything definite.

    If you have something like this instead:
    int a[10];
    int *ip = &a[0];
    then this will be defined; ip will point to a[1], and the command will dereference and postincrement ip, and write 0 to the resulting address (which is a[1]) and leave ip pointing at a[2].

    Without the first ip++ in your original code the behaviour will be defined because ip points to a; *ip++=0 sets a to 0 and increments ip, which then points to somewhere undefined in memory, and dereferencing the pointer again will lead to undefined behaviour.

Share This Page