如何解决c警告:没有强制转换的整数指针?

时间:2014-04-28 09:45:34

标签: 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
}

1 个答案:

答案 0 :(得分:2)

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

解决方案是获取变量的地址:

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

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

另请注意,使用memcpy()将单个字节复制到像这样的字节数组中绝对不会发生在" real" C代码。我只是这样做:

buf[0] = InterruptLatency;