Why won't output 4 in this case?

#include <stdio.h>
int main()
{
    int arr[] = {1, 2, 3, 4, 5};
    int *p = arr;
    ++*p;
    p += 2;
    printf("%d", *p);
    return 0;
}

      

The prefix has precedence ++

over dereferencing, and therefore p

should now point to the second element. And so by adding to it 2

, it should be pointing to the 4th element and the answer should be 4

. But the answer is 3

, why?

+3


source to share


8 answers


In a type expression, ++*p

precedence plays no role at all. In this expression, the inner operator applies to p

( *

in this case). The outer operator ( ++

) is applied to the result of the inner operator.

If you change them to get *++p

, then ++

apply to p

and *

apply to the result ++p

.

Whenever you have a group of unary / postfix operators sitting on the same side of the operand, they are applied in the order of the lowest.



For the example on the right hand side, the p++[i]

statement ++

is applied to p

and [i]

is applied to the result p++

. Meanwhile, the p[i]++

statement [i]

is applied to p

, and ++

is applied to the result p[i]

.

Priority comes into play in "ambiguous" cases such as:

  • Unary / postfix operator versus binary operator like

    *p + 2
    
          

    In the above case, unary *

    has higher precedence than binary +

    , which results in (*p) + 2

    .

    p->i / 5
    
          

    Here postfix ->

    takes precedence over binary /

    , which results in (p->i) / 5

    .

    In general, unary / postfix operators have higher precedence than binary ones.

  • Operator Unary vs. postfix, that is, operators on both sides of the operand, for example

    *p++
    
          

    In the above case, postfix ++

    takes precedence over unary *

    , which results in *(p++)

    .

    &p->i
    
          

    Here postfix ->

    takes precedence over unary &

    , which results in &(p->i)

    .

    In general, postfix operators have higher precedence than unary operators.

  • Various more "exotic" cases such as operator ?:

    ...

+7


source


There is ++*p

no priority issue in the expression , because there is no ambiguity to solve. Operators are applied outward of the operand. That is, it *p

is applied first, but ++

applied to the result of that expression. The language does not allow one operator to jump in front of another.

The precedence rules will matter when both operators can be applied to the same operand, for example

*p++

      



This is not the case in your example.

As for the result, the increment has no effect on p

, which remains to point to the first element arr

until you increment it by p += 2;

. This means that it p

points to arr[2]

which matters 3

.

+2


source


p

to point to the first element arr[0]

.

In the prefix operation, the ++

element that p

points to ( *p

or arr[0]

) is incremented arr[0]

now contains 2 instead of 1. The pointer itself does not change.

p

(the pointer, not the element it points to) then increments by 2, so it p

points to arr[2]

... which matters ... 3.

+2


source


Since the first operation first dereferences and then increments it. Another operation increases the index from 0

to 2

.

If you print arr[0

], you will see that the value has been increased.

See operator preemptive ++

(prefix) and *

(dereferencing). They have the same precision and are in the same cell. As for the legal-variant associativity. They are evaluated as follows:

++(*p)

      

Operators that are in the same cell (there can be multiple lines of operators listed in a cell) are evaluated with the same priority in a given direction. For example, the expression a = b = c is parsed as a = (b = c) rather than (a = b) = c because of right-to-left associativity.

Working example:

#include <stdio.h>
int main()
{
    int arr[] = {1, 2, 3, 4, 5};
    int *p = arr;
    ++*p; 
    p += 2;
    printf("%d", *p);
    printf("%d", arr[0]);
    return 0;
}

      

Output:

32

      

http://ideone.com/6Xlt5N

+2


source


Let me answer:

++*p

      

first searches then increment

*++p

      

increments the address of the pointer and then plays out.

+1


source


If you change the program to a slightly more verbose mode:

#include <stdio.h>

int main()
    {
      int arr[] = {1, 2, 3, 4, 5};
      int *p = arr;
      printf("\n1- %p",p);
      ++*p;
      printf("\n2- %d",arr[0]);
      printf("\n3- %p",p);
      p += 2;
      printf("\n4- %p",p);
      printf("\n5- %d", *p);
      return 0;
    }

      

You will get the following output:

1- 0x7fff9fc53e30
2- 2     // changed the value pointer points to
2- 0x7fff9fc53e30  // pointer stays the same
3- 0x7fff9fc53e38
4- 3

      

+1


source


Pre-permits the order of operations where two operands can bind to the same operand. For example, a 2*x+7

priority *

over +

permit calculation as multiplication procedure before the addition: (2*x)+7

.

In your case only one statement refers to p

, and it dereference operator: *p

. The pre-increment operator works on a dereferenced l-value: ++(*p)

therefore, precedence is irrelevant here.

+1


source


About your question, the code ++*p

is the value *p+1

, not the address

one it points to p

. Correct code ++p

. Hope this helps you.

+1


source







All Articles