Zach
Zach

Reputation: 1

Treating an int as an address/pointer

I have a C function that returns an int that represents the base address of some block in memory. If I wanted to treat the value of that int as an address (so that I could treat it as an array or otherwise traverse the area in memory it points to) what syntax would I use?

Upvotes: 0

Views: 3881

Answers (1)

Trent
Trent

Reputation: 13497

Assuming ints and pointers are the same size on your machine a simple cast should work.

For example:

int function_that_returns_address();
...
char * p = (char *) function_that_returns_address();
p[0] = 'H';
p[1] = 'i';
p[2] = 0;

Upvotes: 4

Related Questions