How are variables related to their values ​​in C?

If you have a line of code like

int num = 4;

Does this work in the following tables?

VARIABLE|ADDRESS   ADDRESS|VALUE
num     |0001      0001   |4

If you would then say

int* num_p = #

Will this be the result of the following tables?

VARIABLE|ADDRESS   ADDRESS|VALUE
num     |0001      0001   |4
num_p   |0002      0002   |0001

Then he will say

int** num_pp = &num_p;

Result in the following tables?

VARIABLE|ADDRESS   ADDRESS|VALUE
num     |0001      0001   |4
num_p   |0002      0002   |0001
num_pp  |0003      0003   |0002

Etc? If so, will the same logic be executed if the initial variable was not int, but instead struct?

EDIT: view comments on this issue for more information on how to look like the address, rather than the scheme 0001, 0002, 0003.

EDIT 2: This answer to this question indicates that the variables do not have to have an address. This answer to an earlier question also goes into this.

+4
source
3

, , , . . " " . , , , .

, true, int, struct?

, a struct - , . , struct , int . , .

, . , , , .

, , .

+7

, int, struct?

- (, int, double, char...), , , , , .

+1

, , , .

The compiler may decide to use case for the variable or the address may be on the stack, in which case it is just an offset to sp(the stack pointer). He can even mix it. The variable in volume and lifetime can be stored by the compiler in the register, then in another register, then moved to memory, and then loaded and stored in the register again. Either just replace it with a value (const propagation) or fully optimize it.

+1
source

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


All Articles