3

In am doing some experiment in C pointers and trying to understand its behaviour. The following are my assumptions for the below codes. Correct me if I am wrong. I have the following codes:

int n[5] = {3,6,9,12,15};   
int *ptr = n;
ptr++;

printf("%d", *ptr);  //Needless to say, the output will be 6

My assumption: The output above is 6 because ptr++ means ptr = ptr + 1 I am changing the value of ptr which is the address of n[0].


Now we take a look at the following scenario:

int n[5] = {3,6,9,12,15};   
int *ptr = n;
*ptr++;

printf("%d", *ptr);  //Why is the output still 6?

My question is: How do we interpret *ptr++? Does it means:

  • *ptr = *ptr + 1 or
  • *ptr = ptr + 1 or
  • ptr = ptr + 1 or what?

By the way, when I print out values of n, it is still 3,6,9,12,15.


Sorry for 2nd question:

How shall we interpret *++ptr and ++*ptr then?

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • The second code should give you a warning that you don't use a calculated value or some such: You dereference `p` before incrementing it, but don't do anything with the dereferenced value. If you want to inrement the value pointed to, use `(*ptr)++`. – M Oehm Nov 19 '14 at 08:38
  • 1
    Whenever it is not obvious which operation is performed first, use parentheses to make it explicit. – August Karlstrom Nov 19 '14 at 08:44
  • 2
    possible duplicate of [Difference between ++\*argv, \*argv++, \*(argv++) and \*(++argv)](http://stackoverflow.com/questions/26902462/difference-between-argv-argv-argv-and-argv) – Ja͢ck Nov 19 '14 at 08:49
  • @Jack somehow the link your pasted "http://stackoverflow.com/questions/26902462/difference-between-argv-argv-argv-and-argv" received a lot of negative votes without many people answering his question. – user3437460 Nov 19 '14 at 08:52
  • 1
    @user3437460 It really only needed one proper answer and that has been given. – Ja͢ck Nov 19 '14 at 08:54
  • Does asking a seemingly similar question justify a down vote?? Will really appreciate if the down-voter leave a comment to justify his down vote. – user3437460 Nov 19 '14 at 09:05
  • the precidence of * is higher than ++, so the result is access the first entry in the n array, do nothing with it, increment p – user3629249 Nov 19 '14 at 09:24
  • @user3629249, This is wrong ++ has higher precedence than * – Milan Jun 27 '20 at 17:58

7 Answers7

4

Expression

*ptr++;

have value

*ptr

before incrementing ptr and then ptr is increment itself. There is no sense to write

*ptr++;

because the value of expression that is *ptr before incrementing of ptr is not used. So in fact the result of these expressions (expression-statements)

ptr++;

and

*ptr++;

is the same.

As for expressions *++ptr and ++*ptr then in this expression *++ptr at first ptr is incremented ( that is it will point to the second element of the array) and then dereferenced and its value is the value of the second element. In this expression ++*ptr at first the value of the first element of the array is returned (that is 3) and then this value is incremented and you will get 4.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks for your answer, was looking for an answer something similar to yours which explains various scenario. +1. – user3437460 Nov 19 '14 at 09:04
2

Please check the link below: Difference between ++*argv, *argv++, *(argv++) and *(++argv)

You need to know about operator precedence in-order to understand what is going on here.

++ operator has higher precedence over *

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • No. ++ operator only has higher precedence over * operator if it is postfix increment. If the ++ operator is prefix increment, it has the same precedence as * operator. – SJHowe May 14 '20 at 14:04
1

Both ptr++ and *ptr++ increment the pointer after they have returned, in the first case, the previous address to which ptr was pointing, and in the second case, the value from this address. You do not do anything with the results, so you do not see the difference.

*++ptr will first increment ptr, then returns the value to which it now points.

++*ptr will get the value to which ptr points, increment it, and then return.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
1

The order of precedence is also compiler dependent. It may change depending upon compilers. Better use a parenthesis to be sure of output

0

Since the previous answers contradict each other I found out.

// gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)
#include "stdio.h"

int main()
{
    int array[] = {0,10,20,30,40,50,60};
    int* p = &array[1];
    int r;
    int i = 1;

    r = *p;
    printf("%d, r = *p: r=%d, p points to %d\n", i++, r, *p);
    r = *p++;
    printf("%d, r = *p++: r=%d, p points to %d\n", i++, r, *p);
    r = ++*p;
    printf("%d, r = ++*p: r=%d, p points to %d\n", i++, r, *p);
    r = *++p;
    printf("%d, r = *++p: r=%d, p points to %d\n", i++, r, *p);
    r = ++*p++;
    printf("%d, r = ++*p++: r=%d, p points to %d\n", i++, r, *p);

    // ++p++;
    printf("%d, ++p++: error: lvalue required as increment operand\n", i++);
    // r = *++p++;
    printf("%d, *++p++: error: lvalue required as increment operand\n", i++);
    // (++p)++;
    printf("%d, (++p)++: C error: lvalue required as increment operand\n", i++);
    printf("%d, ++p++: C++ p points to %d\n", i++, *p);

    // ++(p++);
    printf("%d, ++(p++): error: lvalue required as increment operand\n", i++);

     return 0;
}

Output:

1, r = *p: r=10, p points to 10
2, r = *p++: r=10, p points to 20
3, r = ++*p: r=21, p points to 21
4, r = *++p: r=30, p points to 30
5, r = ++*p++: r=31, p points to 40
6, ++p++: error: lvalue required as increment operand
7, *++p++: error: lvalue required as increment operand
8, (++p)++: C error: lvalue required as increment operand
9, ++p++: C++ p points to 60
10, ++(p++): error: lvalue required as increment operand

1 dereference as expected

2 dereference p and then increment it

3 increment the location pointed to by p, p unchanged

4 increment p and then dereference it

5 increment the thing pointed to by p (++*p) and then increment p (p++)

6,7, 10 can not increment an intermediate value because it does not have a storage location.

8, 9 are "interesting". The C compiler complains about the lack of an lvalue but C++ does not. Instead, as might be expected, it increments p twice. However p += 2 is preferable as it is portable and easier to understand.

-1

It is due to operator precedence in C. The below link may help you

Increment or decrement operator has higher precedence that dereference operator.So your

*ptr++; is similar to *(ptr++)

http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

arunb2w
  • 1,196
  • 9
  • 28
-1

Because of the operator precedence *pt++; is the same as *(ptr++); so it increments the pointer, then dereference the pointer and does nothing.

bitcell
  • 921
  • 8
  • 16