gpt4 book ai didi

C中uint8和char之间的转换

转载 作者:行者123 更新时间:2023-12-02 04:13:24 27 4
gpt4 key购买 nike

我有一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com