Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Clarification with pointer arithmetics eg *ip++ = 0 (http://www.go4expert.com/forums/clarification-pointer-arithmetics-eg-ip-t15152/)

briff 16Nov2008 19:24

Clarification with pointer arithmetics eg *ip++ = 0
 
hi

if i have a code
Code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int a=10, *ip;
    ip =&a;
    ip++;
    printf("ip is %d and a is %d\n",ip,a);
   
    *ip++ = 0;
   
   
    system("pause");
    return 0;
   
}

what do the code in bold do?

thanks!

briff 16Nov2008 19:26

Re: Clarification with pointer arithmetics eg *ip++ = 0
 
sorry the quote i bolded it is actually this:

Code:

*ip++=0

shabbir 16Nov2008 20:22

Re: Clarification with pointer arithmetics eg *ip++ = 0
 
So whats your query?

xpi0t0s 18Nov2008 05:47

Re: Clarification with pointer arithmetics eg *ip++ = 0
 
It's undefined, because ip doesn't point to anything definite.

If you have something like this instead:
Code:

int a[10];
int *ip = &a[0];
ip++;
*ip++=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.


All times are GMT +5.5. The time now is 16:01.