在struct指针中查找value的地址

Tim*_*Tim 0 c++ struct pointers

我有一个名为message的结构:

typedef unsigned char messageType;
struct message{
    message() : val(0), type(home),nDevice(0) {}
    messageType type;
    _int32 val;
    char nDevice;
};
Run Code Online (Sandbox Code Playgroud)

我有一个指向该结构的指针:

message* reply;
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得reply.val的地址,以便我能记住它?例如:

    memcpy(inBuf+2,address here,4);
Run Code Online (Sandbox Code Playgroud)

Yu *_*Hao 5

memcpy(inBuf+2, &reply->val, sizeof(reply->val));
Run Code Online (Sandbox Code Playgroud)

会这样做,因为优先级->高于地址&.

如果您不确定运算符优先级,只需使用括号,可读性更重要:

memcpy(inBuf+2, &(reply->val), sizeof(reply->val));
Run Code Online (Sandbox Code Playgroud)

感谢@ DyP的评论,请注意使用它sizeof(reply->val)比使用文字更好4.