在结构指针中查找值的地址
Find address of value in a struct pointer
我有一个名为message的结构:
typedef unsigned char messageType;
struct message{
message() : val(0), type(home),nDevice(0) {}
messageType type;
_int32 val;
char nDevice;
};
我有一个指向该结构的指针:
message* reply;
我怎样才能得到 reply.val 的地址,以便我可以对它进行记忆?例如:
memcpy(inBuf+2,address here,4);
memcpy(inBuf+2, &reply->val, sizeof(reply->val));
会这样做,因为->
的优先级高于&
地址。
如果您不确定运算符优先级,只需使用括号,可读性更重要:
memcpy(inBuf+2, &(reply->val), sizeof(reply->val));
感谢您@DyP的评论,请注意,使用sizeof(reply->val)
比使用文字4
更好。
尝试&(reply->val)
.
但是,请注意,编译器可以向结构添加填充,这可能会破坏您的计划。
memcpy(&(reply->val), address, sizeof(address))
相关文章:
- 如何为DNS查找反向IPv6地址-C++
- 如何使用C或C 查找给定的IPv6地址是否属于CIDR范围
- 在 c++ 中按地址查找可变名称
- C++如何使用读取进程内存查找进程内存中使用的最后一个(偏移量 - 地址)
- 仅通过参数查找重载地址
- 如何查找auto_ptr的地址
- 查找最低值.功能.错误:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x7fff5fc89000)
- 为游戏制作地址查找器
- 是否可以使用内联汇编在 Visual Studio 2010 c++ 中查找代码的地址?
- 从地址中查找堆或堆块或段
- 查找地址属于哪个堆
- 如何使用字符指针查找字符串的地址
- 查找 DNS 服务器和网关的 IP 和 MAC 地址
- 如何在 64 位 DLL 中查找导出函数的地址?
- 使用GDB查找函数对应的内存地址/调试
- 如何查找继承类的分配地址
- 在 gdb 中查找指向地址的所有变量
- 如何查找 IP 地址是否为链路本地地址
- 从计算机名称中查找计算机 IP 地址
- 使用原始内存地址查找指针/类