- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面是简单的代码:
unsigned char buf[255];
unsigned char msg[255];
while((res = read (fd, buf, (sizeof buf)-1)) > 0)
{
buf[res] = '\0';
strcat(msg, buf);
length += res;
if(length >= 10)
break;
}
这一行 strcat(msg, buf);
给出以下警告:
warning: pointer targets in passing argument 1 of ‘strcat’ differ in signedness
我已经尝试了为类似问题找到的所有解决方案,但没有太大帮助。当两个变量具有相同的符号时,任何人都可以帮助为什么这会出错。我怎样才能删除它。谢谢
编辑:
unsigned const char *buf[255];
unsigned char *msg[255];
strcat(msg, buf);
现在它发出警告:
warning: passing argument 2 of ‘strcat’ from incompatible pointer type
最佳答案
扩展我的 comment变成类似于答案的东西。
strcat()
函数需要普通的 char *:
char *strcpy(char * restrict s1, const char * restrict s2);
您正在通过 unsigned char *
.这些是不同的类型(即使普通 char
是无符号类型)。 str*()
函数的设计目的不是采用 unsigned char
的数组.要“安抚”(或“强制”)编译器接受调用,您需要转换指针:
strcat((char *)msg, (char *)buf);
或者编写并使用一系列自动执行此操作的函数:
static inline unsigned char *ustrcat(unsigned char *dst, const unsigned char *src)
{
return (unsigned char *)strcat((char *)dst, (char *)src);
}
如果您需要经常使用这些功能,这可能是个好主意;这意味着您不必在代码周围留下太多难看的转换。您可以添加 restrict
如果您愿意,可以使用限定词。
注意三种类型 char
, unsigned char
和 signed char
是不同的,即使是普通的 char
与其他两个中的一个具有相同的值范围。 ISO/IEC 9899:2011 §6.2.5 类型 说:
¶15 The three types
char
,signed char
, andunsigned char
are collectively called the character types. The implementation shall definechar
to have the same range, representation, and behavior as eithersigned char
orunsigned char
.45)45)
CHAR_MIN
, defined in<limits.h>
, will have one of the values0
orSCHAR_MIN
, and this can be used to distinguish the two options. Irrespective of the choice made,char
is a separate type from the other two and is not compatible with either.
您的代码容易受到缓冲区溢出的影响,因为您没有在任何地方检查长度。这与您收到的编译警告无关,但您应该确保没有将太多数据复制到目标缓冲区。
你也试过:
unsigned const char *buf[255];
unsigned char *msg[255];
strcat(msg, buf);
现在您将指针传递给一个函数,该函数只需要指向 char
的指针— 编译器有权提示。
关于c - 警告 : pointer targets in passing argument 2 of ‘strcat’ differ in signedness,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43992991/
这个问题在这里已经有了答案: implicit declaration of function itoa is invalid in c99 (3 个答案) C strcat - warning:
这个问题在这里已经有了答案: Why do I first have to strcpy() before strcat()? (5 个答案) 关闭 3 年前。 如题,我做了一个demo来说明一下我
我在使用下面的程序时遇到问题。我正在尝试扫描用户输入的特定单词的字符串命令。我现在的主要问题是,当我运行以下命令时,我收到一条警告,指出“传递 ‘strcat’ 的 arg 2 使指针来自整数而不进行
我知道发生这个 valgrind 错误是因为我试图使用未初始化的东西。下面的代码是导致此错误的代码。它正在做的是尝试读取 Racket 代码并获取每个符号,例如 + 或 define。 (标记化)我不
原型 extern char *strcat(char *dest,char *src); 用法 &n
让我们举个例子。 #include #include int main() { char str1[7] = "hello "; printf("Initial size of s
我试图在不改变单词顺序的情况下颠倒一个句子, 例如:"Hello World"=> "olleH dlroW" 这是我的代码: #include #include char * reverseWo
根据Allocate string (char*) in another function我现在有这样的代码: void someFunction(char** string, char** arg
我想不通。当我在我的 Windows 机器上使用 Code::Blocks 编译这段代码时,它工作得很好,但是当我尝试在 Cygwin 或学校的实际 Unix 机器下使用 Make 编译它时,我得到下
我在一些遗留代码中发现了这一点。 static char title1[] = "SUMMARY REPORT"; static char title2[] = "PERIOD: "; ... str
我正在尝试使用 C 中的指针和 strcat。这是我学习过程的一部分。 想法是用户输入一个包含数字的字符串,输出应该只返回数字。所以,如果用户输入te12abc 输出应该是 12。 这是我的第一次尝试
在对字符串进行一些程序时,我遇到了这个小问题。他们向我提出的问题是——编写函数 strcat(s,t) 的指针版本,它将字符串 t 复制到 s 的末尾。我把程序写成这样 - #include void
我在 strcat 和段错误方面遇到了一些问题。错误如下: Program received signal EXC_BAD_ACCESS, Could not access memory. Reaso
当运行以下故意堆栈粉碎代码时,strcat 将 source 的值复制十次。 #include #include int main() { char a[16]; char b[1
我在 C 中有一个函数,我试图从两个不同的位置(未知大小,可能很大)获取字符串并将它们组合成一个字符串并返回它们。如果我只打印两个字符串,那么我会得到正确的结果,但是当我尝试使用 strcat 组合字
void mystrcat(char* to, const char* from) { while (*to) to++; while (*from) *to++ = *from++;
这是 295c 的问题之一 #include #include main() { char *a="kammo DJ";
我需要用 C 语言编写一个 strcat。我尝试了以下操作: char * c_strcat( char * str1, const char * str2) { char * ret
我正在从文件中读取行,并且我决定按 char 逐个读取 char 中的部分内容。这是我得到的: char str[500]; // it has to be this size, I promi
这个问题已经有答案了: Can I copy a string in an empty string? (3 个回答) 已关闭 8 年前。 自学C语言充满惊喜。我做这个简短的片段来测试 strcat(
我是一名优秀的程序员,十分优秀!