I am learning C language and quite confused the differences between ++*ptr
and *ptr++
.
For example:
int x = 19;
int *ptr = &x;
I know ++*ptr
and *ptr++
produce different results but I am not sure why is that?
I am learning C language and quite confused the differences between ++*ptr
and *ptr++
.
For example:
int x = 19;
int *ptr = &x;
I know ++*ptr
and *ptr++
produce different results but I am not sure why is that?
These statements produce different results because of the way in which the operators bind. In particular, the prefix ++
operator has the same precedence as *
, and they associate right-to-left. Thus
++*ptr
is parsed as
++(*ptr)
meaning "increment the value pointed at by ptr
,". On the other hand, the postfix ++
operator has higher precedence than the dereferrence operator *
. Thefore
*ptr++
means
*(ptr++)
which means "increment ptr
to go to the element after the one it points at, then dereference its old value" (since postfix ++
hands back the value the pointer used to have).
In the context you described, you probably want to write ++*ptr
, which would increment x
indirectly through ptr
. Writing *ptr++
would be dangerous because it would march ptr
forward past x
, and since x
isn't part of an array the pointer would be dangling somewhere in memory (perhaps on top of itself!)
Hope this helps!
*ptr++
is equivalent to *(ptr++)
equivalent to *(ptr = ptr + sizeof(datatype))
–
Johnsiejohnson *ptr++
will be useful , if your ptr is pointing to a string and you want to jump to next char in the string. –
Axes *(ptr++)
. –
Urnfield ptr + sizeof(datatype)
will actually move past sizeof(datatype) * sizeof(datatype)
bytes. See this example. [Note: Example is compiled as C instead of C++, due to -xc
command-line option.] –
Hettie ptr++
is the same as ptr = ptr + 1
meaning ptr
will jump to the next address (that is what adding 1 means in the context of pointers), in particular jump to an address that is sizeof(datatype)
bytes away from the starting address. As @Justin Time - Reinstate Monica explained, something like ptr = ptr + 3
would mean to jump to the address which is 3*sizeof(datatype)
bytes away. So indeed ptr = ptr + sizeof(datatype)
means to jump to the address which is sizeof(datatype) * sizeof(datatype)
bytes away –
Nika The accepted answer is not correct. It's not the case that the postfix ++
operator has the same precedence as dereference/indirection *
. The prefix and postfix operators have different precedence, and only the prefix operator has the same precedence as dereference/indirection.
As the precedence table shows, postfix ++
has a higher precedence than dereference/indirection *
. So *ptr++
gets evaluated as *(ptr++)
. ptr++
evaluates to the current value of ptr
; it increments ptr
only as a side effect. The value of the expression is the same as the current value of ptr
. So it won't have any effect on the value stored at the pointer. It will merely dereference the pointer (i.e., get the current value stored there, which is 19), then advance the pointer. In your example there is no defined value stored at the new position of ptr
, so the pointer is pointing to garbage. Dereferencing it now would be dangerous.
Also as the table shows, prefix ++
has the same precedence as dereference/indirection *
, but because of right-left associativity, it gets evaluated as ++(*ptr)
. This will first dereference the pointer (i.e., get the value stored at the address pointed to) and then increment that value. I.e., the value will now be 20.
The accepted answer is correct about the effects of the two, but the actual mechanism is different from the one given there.
ptr++
evaluates to ptr
. –
Constrict *ptr
returns an l-value. So ++*ptr
will absolutely modify the value of the object pointed by ptr
(x
). –
Broadtail As templatetypedef says, but you should provide the parenthesis around *ptr
to ensure the outcome. For instance, the following yields 1606415888 using GCC and 0 using CLang on my computer:
int x = 19;
int *ptr = &x;
printf("%d\n", *ptr++);
printf("%d\n", *ptr);
And you expected x
to be 20. So use (*ptr)++
instead.
© 2022 - 2024 — McMap. All rights reserved.