【问题标题】:incompatible type C error?不兼容的 C 型错误?
【发布时间】:2014-04-28 08:37:22
【问题描述】:
uint32 InterruptLatency;
uint8 measurements[32];
char buf[256];
int kernelinterrupt time()
{
fscanf(fp,"%lu", InterruptLatency);  // I am reading the data from kernel which is not shown here
measurements[17] = InterrupLatency;
buf = &measurements;            // I am getting error here as below

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

错误:从 uint8 (*)[32] 类型分配 char[256] 类型时类型不兼容

有人可以帮我解决这个问题吗??

【问题讨论】:

  • 能不能写个带空格的函数名?
  • by buf = &measurements;你打算做什么?
  • 错误信息包含所有信息。
  • 请不要在提供答案后更改您的问题,因为这可能会使某些答案难以理解。将修改添加到您的问题作为更新。
  • 参考我上面的评论,我回滚了你问题的最后修改。

标签: c arrays pointers memory interrupt


【解决方案1】:

在 C 中,您不能分配数组。您必须明确复制内存。

可能你想这样做:

memcpy(buf, measurements, sizeof(measurements));

但你没有详细说明你真正想做的事情。

PS:你的fscanf() 是错误的。它应该采用将保存读取值的变量的地址。

如果你使用 uint32_t,你应该使用 SCNu32 规范,来自 <inttypes.h>,以确保你不会破坏:

fscanf(fp,"%"SCNu32, &InterruptLatency);

【讨论】:

  • 另外,"l" 长度修饰符意味着在 OP 的平台上,longs 预计为 32 位。是这样吗?
  • @alk:OP 是什么意思?很多人写OP。我用谷歌搜索了,但我没有得到确切的信息?
  • @alk:你是对的,正确的方法是使用来自<inttypes.h>的宏。已更正。
  • @user3458454:如果变量是uint32_t,则不能保存 64 位值。为此使用uint64_tSCNu64
  • 我问了一个新问题:stackoverflow.com/questions/23336896/…
【解决方案2】:

您正在尝试将指针值分配给数组。你不能那样做。

使用 memcpy:

memcpy(buf, &measurements, sizeof(measurements));

【讨论】:

    猜你喜欢
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 2013-10-14
    • 2012-10-19
    • 1970-01-01
    • 2013-09-23
    相关资源
    最近更新 更多