警告:传递“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;

09-26 01:07