【问题标题】:Why this code returns the following error after compiling "invalid type argument of unary '*' (have 'int')"?为什么这段代码在编译\"invalid type argument of unary \'*\' (have \'int\')\"后返回以下错误?
【发布时间】:2023-01-24 03:09:40
【问题描述】:

给定一个包含在三个 8 位寄存器中的 24 位字符串,假设: 数组[0]、数组[1]、数组[2] 其中 array[0] 的第一位是 MSB,array[2] 的最后一位是 LSB。我的目标是将此字符串转换为整数值。

为此,我编写了以下代码:

uint32_t bin2Int(uint8_t array[]) {
    uint8_t arr0[3];
    uint8_t mask = 0x80;
    uint8_t outputData[24];
    uint8_t s;
    uint8_t k = 23;
    uint32_t IntResult = 0;
    for (s=0; s<3; s++) {
      arr0[s] = *array[s];
      while (mask > 0) {
          outputData[k] = arr0[s] & mask;
          mask >>= 1;
          k--;
      }
      mask = 0x80;
    }
    for (s=0; s<24; s++) {
      if(outputData[s] != 0)
          IntResult += pow(2, s);
    }
    return IntResult;
}

此功能在STM32CubeIde中实现,当我构建项目时,编译器返回以下错误:“一元'*'(具有'int')的无效类型参数”。 在我的项目中添加此代码之前,我在 DevC ide 中对其进行了测试并且它工作正常,但是当我在 STM32CubeIde 上尝试它时它返回此错误。我的代码有什么问题?非常感谢你。

【问题讨论】:

  • array[s] 是一个整数,你为什么要用*array[s] 取消引用它?

标签: c pointers microcontroller


【解决方案1】:

在这份声明中

arr0[s] = *array[s];

右侧表达式的指针类型为uint8_t *,而左侧表达式的类型为uint8_t。

看来你的意思

arr0[s] = array[s];

【讨论】:

    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多