Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ++(*p) changes the pointer value?

Tags:

c++

c

pointers

If I have this code:

int A[5] = { 2, 1, 3, 55 };
int *p = A;
cout << ++(*p);

the result is 3 and the value of the first position of A is 3 also, why?

I mean, by hierarchy of operators () is more hierarchical than ++, then we would need operate *p first:

++(*p) => ++(2) => 3 

with any change in A vector?

like image 760
Kintaro Oe Avatar asked Jul 13 '20 08:07

Kintaro Oe


People also ask

Can the value of a pointer be changed?

Modifying the value of a pointerWe can change the pointer's value in a code, but the downside is that the changes made also cause a change in the original variable.

What does * and indicate pointer?

double** means a pointer to a pointer to a double variable. In the case of the function you posted, it is used to create a sort of two-dimensional array of doubles.

What is the difference between P and * p in pointers?

p is the value of p while *p is the value stored in the memory location pointed by p . When you want to indirectly access the value of an integer i , you can have an integer pointer point to it ( int *p = &i ) and use that pointer to modify the value of i indirectly ( *p = 10 ).

Can a pointer address change?

You can't change the address of a pointer (or of any variable). Once you declare the variable, it's address is firmly set, once and forever. You can, however, change the value of a pointer, just as easily as you can change the value of an int: x = 10 or p = &t.


2 Answers

*p is not just "2", it's an lvalue, i.e. this "2" has a well-defined location. The value at this location is modified by the ++ operator - by definition of the ++ operator.

If you don't want to modify the value, use + 1 instead: *p + 1.


In C/C++, lvalue is a value with a defined location in memory. This value can be changed - by an assignment, incremented, decrement. For example,

int x = 0;
x = 1; // ok, x is an lvalue, assignment changes the value from 0 to 1

int *p = &x;
*p = 2; // ok, *p is an lvalue, assignment changes the value from 1 to 2

In contrast, an rvalue is a value without a defined location - for example, a result of an arithmetic operation. This value can't be assigned, incremented or decremented (it doesn't mean it can't be used in a larger expression).

For example,

int x = 0, y = 1;
(x + y) = 3; // compilation error, (x + y) is an rvalue

2++; // compilation error, 2 is an rvalue

Here's a pretty simple article explaining lvalues / rvalues: https://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c

like image 69
Alex Shesterov Avatar answered Oct 18 '22 22:10

Alex Shesterov


The ++(*p) is the same as ++p[0] and ++A[0] All change the first element of the array.

Why ++(*p) changes the pointer value

It does not. The pointer value is value kept in the p. It is called "address" or "reference". It does not change.

like image 21
0___________ Avatar answered Oct 18 '22 22:10

0___________