dte*_*ech 7 c memory casting memory-access
我如何制作这样的作品?
void *memory = malloc(1000); //allocate a pool of memory
*(memory+10) = 1; //set an integer value at byte 10
int i = *(memory+10); //read an integer value from the 10th byte
Run Code Online (Sandbox Code Playgroud)
简单示例:将内存视为unsigned char数组
void *memory = malloc(1000); //allocate a pool of memory
uint8_t *ptr = memory+10;
*ptr = 1 //set an integer value at byte 10
uint8_t i = *ptr; //read an integer value from the 10th byte
Run Code Online (Sandbox Code Playgroud)
您也可以使用整数,但是您必须注意一次设置的字节数.
规则很简单:
由此您可以得出结论,如果您想执行“原始”指针算术,则必须在 char* 之间进行转换。