c - 如何解决c警告:pointer from integer without a cast?

标签 c arrays pointers memcpy

警告:传递‘memcpy’的参数 2 使指针来自整数而不进行强制转换[默认启用]

uint8 InterruptLatency;
char buf[256];
int kernelinterrupt time()
{
    fscanf(fp, "%"SCNu8"\n", &InterruptLatency);  // I am reading the data from kernel which is not shown here
  memcpy(buf, InterruptLatency, sizeof(InterruptLatency));   // warning here as above

    // after storing it in buffer I am sending the data from but to another layer
}

最佳答案

memcpy() 函数需要两个指针,但 InterruptLatency 是一个 8 位整数。

解决方法是取变量的地址:

memcpy(buf, &InterruptLatency, sizeof InterruptLatency);
            ^
            |
        address-of
        operator

请注意,在获取实际对象的大小时,sizeof 不需要括号。这是因为 sizeof 不是函数。

另请注意,像这样使用memcpy() 将单个字节复制到字节数组中永远不会发生在“真正的”C 代码中。我会这样做:

buf[0] = InterruptLatency;

关于c - 如何解决c警告:pointer from integer without a cast?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23337710/

相关文章:

c++ - 函数的 std::vector

c++ - 通过 tcp 传输的文件末尾的额外换行符

C 内在函数、SSE2 点积和 gcc -O3 生成的程序集

c - Q : Reading from pipe to screen

c - 了解 C 语言中的复杂指针示例代码

c - C中指针运算加减错

c - execl 是否需要 dup2()

Javascript 访问多维数组对象

php - 如何获得序列化输出数据?

javascript - 如何让这个功能正常工作呢?