【问题标题】:how to solve the c warning :pointer from integer without a cast?如何解决c警告:没有强制转换的整数指针?
【发布时间】:2014-04-28 09:45:33
【问题描述】:

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

【问题讨论】:

    标签: c arrays pointers memcpy


    【解决方案1】:

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

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

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

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

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

    buf[0] = InterruptLatency;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      相关资源
      最近更新 更多