0

I am afraid I did not get correctly the precedence between operators. The following program works differently than expected:

#include<stdio.h>

int main(){
    char a[] = "abc";
    char *p;
    int i;

    p = a;
    for(i = 0; i < 3; ++i)          /* First case */
        printf("%c\n", *p++);

    printf("a = %s\n", a);

    p = a;

    for(i = 0; i < 3; ++i)        /* Second case */
        printf("%c\n", (*p)++);
    printf("a = %s\n", a);

    return 0;
}

First case: according to this table, *p++ is *(p++). I increment the address pointed by p, then I dereference it. Why it returns abc, then, and not bc + "value beyond string"?

I encounter a similar problem with the second case, with the expression (*p)++. In this case, the parentheses make me increase the dereferentiated value of p. Hence, I expect the expression to return bcd rather than dbc.

Worice
  • 3,847
  • 3
  • 28
  • 49
  • Because `p++` is **post**-increment. – Oliver Charlesworth Nov 30 '17 at 17:15
  • I am ok with the first case, then. In any case, the address pointed by `p` is incremented after being dereferentiated. But what happens in the second case? It 's like the string is "circularly moved forward", if I am able to express myself. – Worice Nov 30 '17 at 17:23
  • `*p++` dereferences the pointer to read what it points to and then increments the pointer. `(*p)++` increments what it points to. – Weather Vane Nov 30 '17 at 17:48
  • In the second case, you're updating the value `p` *points to*, not the value of `p` itself, so you're not iterating through the string - you're just setting the first element of `a` to `'b'`, then `'c'`, then `'d'`. – John Bode Nov 30 '17 at 17:48

2 Answers2

1

In the first case it is performing the pointer increment after it's value is returned, because the operation is a post-increment.

In the second case you are incrementing *p, 3 times in the loop without changing the value of p itself. So the first element of a (which p is pointing to) is incremented 3 times resulting in 'a'+3='d'. The rest of the elements remain unchanged.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
1

FIRST CASE:

Iteration 1: i=0, *p = a, a point to 'abc'. now after this statement p will point to 'b' and i =1

Iteration 2: i=1, *p = b, a point to 'abc'. now after this statement p will point to 'c' and i =2

Iteration 3: I=2, *p = c, a point to 'abc'. now after this statement p will point to null termination char and i=3

Iteration 4: loop breaks, a still points to 'abc'

SECOND CASE:

Iteration 1: i=0, *p = 'a', a points to 'abc'. After this statement the value at which p points increases thus a points to 'bbc' and i=1

Iteration 2: i=1, *p ='b', a point to 'bbc'. After this statement the value at which p points increases thus a points to 'cbc' and i=2

Iteration 3: i=2, *p ='c', a points to 'cbc'. After this statement the value at which p points increases thus a points to 'dbc' and i= 3

Iteration 4: loop breaks and a points to 'dbc' And p points to 'd'

Alex Morrison
  • 405
  • 3
  • 12