V
V
VBKesha2014-05-10 00:17:16
C++ / C#
VBKesha, 2014-05-10 00:17:16

C write value by reference to reference?

There is this code:

int *i;
i=(int *)0x403040;
*i=(int)malloc(sizeof(int));

It seems pretty crazy, but I know for sure that another variable is declared in cell 0x403040, which is a pointer to int and no memory has been allocated for it.
Accordingly, I declare a pointer i and assign it an address known to me, after which I allocate memory for the size of int and write a pointer to this address in i. I check it works as I need. But now I don’t understand how to write any value to the allocated memory area, more precisely, I came to this code:
int *i;
int *z;
i=(int *)0x403040;
z=(int *)malloc(sizeof(int));
*z=31337;
*i=(int)z;

Again, I tested everything and it works exactly as I need it, but I don’t understand how to write it down without entering z.
PS. I'm not very familiar with C, so I'm 90% sure that I don't understand something in the syntax, but I don't understand what.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
one pavel, 2014-05-10
@VBKesha

*((int*)(*i)) = 100500;

T
tsarevfs, 2014-05-10
@tsarevfs

I thought you were trying to invent a pointer to a pointer. Such constructions are supported by the language:

int *i = NULL; //указатель на ячейку типа int
int **pi = &i; //указатель на указатель на int

int *a = (int *)malloc(sizeof(int)); создаем саму ячейку
*a = 10; // записываем в нее значение

*pi = a;//теперь i и a указывают на число 10

free(a); //ячейка помечена как свободная. i и a ссылаются на "мусор"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question