如何从C中的“原始”内存中读取/写入类型值?

前端之家收集整理的这篇文章主要介绍了如何从C中的“原始”内存中读取/写入类型值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我如何制作这样的作品呢?
  1. void *memory = malloc(1000); //allocate a pool of memory
  2. *(memory+10) = 1; //set an integer value at byte 10
  3. int i = *(memory+10); //read an integer value from the 10th byte

解决方法

简单示例:将内存视为unsigned char数组
  1. void *memory = malloc(1000); //allocate a pool of memory
  2. uint8_t *ptr = memory+10;
  3. *ptr = 1 //set an integer value at byte 10
  4. uint8_t i = *ptr; //read an integer value from the 10th byte

您也可以使用整数,但是您必须注意一次设置的字节数.

猜你在找的C&C++相关文章