C语言 警告:传递“__builtin___strncpy_chk”参数 1 中的指针目标的符号不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6414604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:58:38  来源:igfitidea点击:

warning: pointer targets in passing argument 1 of '__builtin___strncpy_chk' differ in signedness

c

提问by Angus

I am getting a warning for the below code.

我收到以下代码的警告。

//someother class 
#define EVENT_ID_DESCRIPTION_LEN  64
struct FILE_DESCRIPTIONS
{
    uint32_t  uFileID;
    uint32_t  uDescriptionLen; 
    int8_t   szDescription[FILE_ID_DESCRIPTION_LEN];
};

//defined inside a function of someother class
int8_t  chTemp[EVENT_ID_DESCRIPTION_LEN + 1];
strncpy(chTemp,pMsg->st.aDescriptions[nIndex].szDescription,EVENT_ID_DESCRIPTION_LEN);

warning: pointer targets in passing argument 1 of '_builtin__strncpy_chk' differ in signedness

警告:传递“_内置__strncpy_chk” 的参数 1 中的指针目标的符号不同

The value from which i'm storing from is also uint8_t and that gets stored onto is also uint8_t.what might be the cause of this warning. Thanks in advance.

我从中存储的值也是 uint8_t 并且存储到的值也是 uint8_t。这可能是此警告的原因。提前致谢。

回答by karlphillip

Actually, your code is defining int8_t(signed int) and not uint8_t(unsigned int) as you think. Did you spotted it?

实际上,您的代码定义了int8_t(有符号整数)而不是您认为的uint8_t(无符号整数)。你发现了吗?

You have to change your variable to:

您必须将变量更改为:

uint8_t  chTemp[EVENT_ID_DESCRIPTION_LEN + 1];

回答by Kerrek SB

Remember that in C, the three types char, unsigned charand signed charare all distinct, and strncpyexpects a char. If your int8_tis defined as signed char, you have conflicting types. Best to use an actual char, non?

请记住,在 C 中,三种类型char,unsigned charsigned char都是不同的,并且strncpy期望一个char. 如果您int8_t的定义为signed char,则您的类型存在冲突。最好使用实际的char,非?

回答by Deepak Danduprolu

Can't you use memcpyinstead?

你不能用memcpy吗?

memcpy(chTemp, pMsg->st.aDescriptions[nIndex].szDescription, EVENT_ID_DESCRIPTION_LEN);