duannuo4620 2017-06-10 20:45
浏览 281
已采纳

Go中的uint32和bool类型不匹配

I have a C macro defined like this:

#define normalize(c, a) c = (a) + ((a) == 0xFFFFFFFF)

I was rewriting it in Go, and as long as I know there is no such things as C macros in Go. Therefore, I created a normal function:

func normalize(a uint32, c *uint32) {
    *c = a + (a == 0xFFFFFFFF)
}

The problem is that this gives me a type mismatch error. Any ideas how to fix it?

  • 写回答

1条回答 默认 最新

  • dp9599 2017-06-10 21:10
    关注

    So your C normalize macro assigns c to a if a is not equal to 0xffffffff, or to 0 otherwise. I'm not sure what kind of normalization it is, but it's not my concern now.

    So given the Go function signature you provided, this would work:

    func normalize(a uint32, c *uint32) {
        if a != 0xffffffff {
            *c = a
        } else {
            *c = 0
        }
    }
    

    However, I'm not sure why not just return a value instead of writing it via c pointer?

    func normalize(a uint32) {
        if a != 0xffffffff {
            return a
        }
        return 0
    }
    

    Side note: the same applies to your C macro. By the way, the macro evaluates a twice, this might come as a surprise if you ever pass some function with side effects as a. Any reason not to use (inline) function instead of a macro, or at least make it so that it evaluates to a new value, instead of assigning c to it?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符