【问题标题】:C Struct with uint8 array weird behavior具有 uint8 数组奇怪行为的 C Struct
【发布时间】:2015-07-09 02:46:45
【问题描述】:

我对以下 C 结构有疑问:

typedef struct AnchorPixel{
    int32 X;
    int32 Y;
    uint8 CH[5];
} AnchorPixel;

其实我里面的 CH 数组有问题。我只是无法操作 CH 数组。比如下面的程序

AnchorPixel a;
a.CH[2] = 5;
cout << a.CH[2];

给出输出:

如果我将 CH 类型从 uint8 更改为 int32,问题就会消失。这有效:

typedef struct AnchorPixel{
        int32 X;
        int32 Y;
        int32 CH[5]; 
    } AnchorPixel;

有什么想法吗?

【问题讨论】:

  • uint8_t is probably typedef'ed as a char ... 我猜这就是uint8 的真正含义。
  • 另外因为我看到cout 我猜这真的应该被标记为C++
  • 你是对的 Shafik Yaghmour
  • 这适用于 AnchorPixel a; a.CH[2] = 5; cout
  • 我对哪条评论正确?关于标记这个 C++ 或关于 typedef?

标签: c++ arrays struct


【解决方案1】:

似乎uint8 被定义为unsigned char,我们can see on coliruuint8_t 就是这种情况。 cstdint 标头包括 stdint.h 并且 uint8_t 确实是 unsigned char 的 typedef:

typedef unsigned char       uint8_t;

您看到的输出与couta.CH[2] 视为char 类型一致,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2016-06-28
    相关资源
    最近更新 更多