Why not the result of this casting?

I need some tips with this weird behavior - let's get this code:

int ** p;

Compiles without problems:

p++;

But this:

((int**)p)++;

It gives me this error message: "error: lvalue required as increment operand".

I turn to pthe type that already exists, nothing changes, so what's the problem? This is a simplified version of the problem that I encountered when I tried to compile one old version gdb. So I suppose it worked and something changed. Any idea what is wrong with the second example?

+4
source share
4 answers

gcc -, "lvalue casts" - , lvalue, lvalue . , :

int *p;
++(char *)p;  /* increment p by one byte, resulting in an unaligned pointer */

gcc v3.0 gcc v4.0

gcc, ( ), :

p = (int *)((char *)p + 1);

, - undefined , , - .

+5

, rvalue, lvalue. , typecast " , , - ", rvalue, lvalue. , ++ typecast, ++ lvalue, rvalue.

, C, lvalue, lvalue, , .

, !

+4

lvalue?

6.5.4 C99, 4, 86, :

lvalue.

.

lvalue.

++ lvalue.

.

+4

C ( ) r. . , , . ( , .)

In fact, one of the fundamental properties of the entire C-language is that it always converts lvalues ​​to r values ​​in expressions as quickly as possible. Lvalues ​​in C expressions are similar to the 115th element of the periodic table: they usually live a very short life, quickly decomposing into rvalues. This is the main difference between C and C ++, with the latter always trying to keep lvalues ​​in expressions as long as possible (although in C ++ this particular cast will also produce an rvalue).

+3
source

Source: https://habr.com/ru/post/1523929/


All Articles