user11043025
user11043025

Reputation:

How to reset a pointer

After playing with a pointer given by malloc.

uint8_t* mem = malloc(10);
uint8_t* rst = mem;

*mem++ <<= 3;
 // etc...
 mem = rst;

Is there any other "elegant" way of resetting a pointer to the first element of the ram given by malloc than to have previously copied its value just after the allocation ?

Upvotes: 2

Views: 946

Answers (1)

jxh
jxh

Reputation: 70472

In this particular case, you could use rst to do your pointer adjustments, and leave mem alone.

*rst++ <<= 3;
 // etc...

If the modifications you are making make sense as a functional unit, then you can encapsulate the code in a function call, and avoid declaring rst altogether.

extern void adjust_mem(uint8_t *);

uint8_t* mem = malloc(10);

adjust_mem(mem);

Upvotes: 3

Related Questions