C中uint8和char之间的转换

标签 c arrays char uint8t

我有一个 API 可以实现对 EEPROM 的写入操作。这是它的声明:

CYBLE_API_RESULT_T CyBle_StoreAppData (uint8 * srcBuff, const uint8 destAddr[], uint32 buffLen, uint8 isForceWrite);

当我调用此函数并将数组参数发送到 srcBuff 时,它运行良好。已声明为 uint8类型。

问题是,我需要发送char指向它的数组指针。我在想char已经是 uint8 ,但如果我发送 char ,我会收到编译器警告指向该函数的数组指针而不是 uint8 。为什么我不能使用char而不是uint8 ?以下是调用该函数的 2 个示例:

static const uint8      datastack_ROM[dedicatedRomSize] = {0};
uint8                   Container_ID[10];
char                    Prefix[10];

//Call the function with Container_ID which has been declared as uint8. This is working.
CyBle_StoreAppData(Container_ID,datastack_ROM,10,0);

//Call the function with Prefix which has been declared as char. This is NOT working.
CyBle_StoreAppData(Prefix,datastack_ROM,10,0);

这是第二次调用的警告:

passing char[10] to parameter of type 'uint8 *' converts between pointers to integer types with different sign.

不是charuint8一样吗?

最佳答案

两种类型的长度都是 8 位。区别在于符号。

  • uint8 类型是无符号的。
  • char 类型应根据您的情况进行签名。实际上,它取决于编译器,但大多数编译器默认将 char 类型视为有符号类型,并且可以选择在需要时强制将 char 类型强制为无符号类型。请参阅C99 standard document reference §6.2.5p15:

The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.

CHAR_MIN, defined in limits.h, will have one of the values 0 or SCHAR_MIN, and this can be used to distinguish the two options.

关于C中uint8和char之间的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35264923/

相关文章:

python - 如果索引模数 7 == 0,则从每个 block 中提取每个字符

c - C语言初始化换行符

c - 'N'字符出现在字符串的末尾

php - php 中的函数 array_multisort 更改我的数组的键

java - 不带循环的整数数组交集递归

arrays - 数组如何在运行时快速重新分配内存?

c - 如何在 Xcode 4 中创建一个新的 C 文件?

c - 使用 fopen 写入文件不起作用

c - Ruby 中没有操作范围?

c - C语言中为什么char是1个字节