Pointer Dereferencing

Prett y basic task with pointers, but I'm tearing my hair out.

1
2
3
4
5
char *heap_listp;
char *first_free;

heap_listp = &something;
first_free = heap_listp + 8;


This all works just fine. But then I want to store the value stored in heap_listp in the place where first_free points to (offset by 4 bytes). (Note: I can't use &something - this is just an illustration).

So I type:
*(first_free + 4) = heap_listp;

but I get this error:
assignment makes integer from pointer without a cast


I don't get it? What am I doing wrong?

Thanks!


Last edited on
*(first_free + 4) = heap_listp;
The right operand is type char pointer, but the left is type char cuz it's dereferenced. They don't match.
Do this instead:
*(first_free + 4) = *heap_listp;
Thanks. That makes the program compile, but it doesn't do what i want. That takes the value stored at where heap_listp points to and copies it to where first_free points to (offset by 4 bytes). I don't want that value. I want the value of the pointer itself.

Say heap_listp points to memory address 0xeb649010. At that address is the number 2. first_free points to the address 0xeb649018. In memory location 0xeb64901c, I am trying to store the *value* 0xeb649010. The change you suggested places the value 2 at memory location 0xeb64901c. Make sense????

Thanks for your help!
Last edited on
You could use a pointer to pointer.
You should also look into void pointers, but I won't complicate the example.

1
2
3
4
char **ppv;

ppv  = reinterpret_cast<char **> (0xeb64901c); // force pointer's value
*ppv = reinterpret_cast<char *> (0xeb649010); // store memory address value there 


Disclaimer: the code above was not tested.

Edit: obvious bug fixed.
Last edited on
1
2
char ** place = ((char **) first_free) + 4;
*place = heap_listp;

You can do that because pointers to a type can be converted freely to pointers to other types (as long as you know what you are doing).
Topic archived. No new replies allowed.