简体   繁体   English

指向正确的内存位置的指针

[英]pointers to point proper memory location

I am writing a bit of C code in which I am presented with following things: 我正在编写一些C代码,其中包含以下内容:

typedef uint8_t BYTE;
typedef int8_t SBYTE;
typedef uint16_t WORD;
typedef uint32_t DWORD;
typedef uint64_t QWORD;

I have a function having definition as QWORD exp_d(const DWORD *in) . 我有一个定义为QWORD exp_d(const DWORD *in)

And I have a pointer to a QWORD as QWORD* input . 我有一个指向QWORD的指针,作为QWORD* input Being a QWORD, it is of 64 bits. 作为QWORD,它是64位。 Now, I want to send the least-significant 32 bits of input to function exp_d . 现在,我想的最低显著32位发送input到功能exp_d What I am doing is, 我在做的是

QWORD expansion=exp_d(((DWORD*)input)+1);

I think, input is a QWORD* , so first typecasting it to a DWORD* , and then incrimenting it by 1 (to get to next DWORD , ie least significant 32 bits of QWORD ) should do the thing. 我认为, inputQWORD* ,因此首先将其类型转换为DWORD* ,然后将其递增1(以获取下一个DWORD ,即QWORD最低有效32位)。 However, when I pass such value to exp_d , I get the most significant 32 bits of input rather than least significant 32 bits as expected. 但是,当我exp_d值传递给exp_d ,我得到的是input的最高有效32位,而不是预期的最低有效32位。

Where am I going wrong? 我要去哪里错了?

如果输入的类型是QWORD ,则使用数字值:

(uint32_t) ((*input) & 0xffffffff)

If function exp_d can't be modified, then you might need to define another function which return the pointer to lower 32 bits from 64 bits pointer. 如果无法修改函数exp_d ,则可能需要定义另一个函数,该函数将指针从64位指针返回至低32位。 Inside function, return address according to your system endianness. 在函数内部,根据您的系统字节序返回地址。

DWORD * GetDwordFromQword(const QWORD *input)
{
    if(BigEndian()) {
        return (DWORD*)input+1;
    } else {
        return (DWORD*)input;
    }
}

BigEndian() definition can refer here Detecting endianness programmatically in a C++ program BigEndian()定义可以在此处引用以编程方式在C ++程序中检测字节序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM