0
int main(void)
{
    int n1 = 2, n2 = 5;
    int *p = &n1, *q = &n2;
    *p = *(q++);
    printf("%d,%d", *p, *q);
    return 0;
}

output= 5,5

Why the value of *q is 5 it should have some garbage value?

int main(void)

{

    int n1 = 2, n2 = 5;

    int *p = &n1, *q = &n2;

    *p = *(++q);

    printf("%d,%d", *p, *q);

    return 0;

}

output= 2,2

And how is this happening? Can anyone explain how precedence rule works in pointers?

Shubham Tiwari
  • 392
  • 1
  • 2
  • 15
  • 6
    What makes you think 5 is not 'some garbage value'? – ach Sep 06 '15 at 20:24
  • Because this coincidence can happen 1 or 2 times but not every time i run the program........ – Shubham Tiwari Sep 07 '15 at 06:13
  • There is no reason why the program should not produce consistent results on every run. 'Garbage value' doesn't mean there is a random number generator to produce it. It means some sort of leftover from other activities the program performs on the stack. In a program that performs the same activities on every run, that leftover is likely to be the same as well. – ach Sep 07 '15 at 06:22
  • I have posted another piece of code similar to previous one,but with a little difference can you explain the behavior of this program's output. – Shubham Tiwari Sep 07 '15 at 06:43
  • The only way to explain why it is working for you the way it is working is to ask your compiler to produce assembly output and then to analyze it. Unlike C, assembly language always has unambiguos interpretation. You might also try to dump the produced object file in disassembly mode. – ach Sep 07 '15 at 06:58
  • Can you tell me how will i do this???? – Shubham Tiwari Sep 07 '15 at 08:05
  • Depends on which compiler you're using. – ach Sep 07 '15 at 08:07

2 Answers2

6

*p = *(q++); is (more or less) equivalent to *p = *q; q++;, so p is fine. q++ will be evaluated, yielding the old value of q (i.e. the value pre-increment). What you're seeing there is the expected behavior.

You do have undefined behavior in the deference of q in the printf call though since q no longer points at memory you own at that point. A million different things could be causing that (e.g. last time the memory was allocated, maybe a 5 was there, the compiler is being too nice and trying to help you, etc), but you cannot and should not depend on this behavior. Doing so is dangerous, and this program would likely crash or output nonsense on many compilers/operating systems/hardware.

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • @ChrisBeck Whoops, not sure how i missed that. Will update accordingly. – Corbin Sep 06 '15 at 20:33
  • 2
    But when he then `printf`'s the value `*q`, that is UB, because `q` was incremented and is not pointing at anything valid anymore. @Corbin: Yeah, it's a weird example. – Chris Beck Sep 06 '15 at 20:33
1

Why the value of *q is 5 it should have some garbage value?

It is due to the postfix incrementation in *(q++) which acts after the pointer dereference and the assignment to *p.

So, the current value of the address pointed by *q is assigned to *p, and then q is incremented to a "garbage value".

This strange result in printing 5,5 is undefined behaviour.

Jens
  • 69,818
  • 15
  • 125
  • 179
Ziezi
  • 6,375
  • 3
  • 39
  • 49