我正在读取一个 fifo,然后尝试用它创建另一个不同格式的数组。 我知道 buf 按预期获取其值,因为将其打印出来很好。
char graphic[100];
char buf[100];
read(fd_read,&buf,50);
for(int i=0; i<10; i++){
strcat(graphic, buf[i]); }
给我错误:
passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
我在 Linux 上使用 eclipse,在 C 上。 ...帮助?
最佳答案
strcat
的签名是:
char *strcat(char *dest, const char *src);
即第二个参数应该是指向 char
的指针以 '\0'
结尾的数组。在您的情况下,您正在传递一个 char
角色。
而不是 for(int i=0; i<10; i++){ strcat(graphic, buf[i]); }
,您可能只需要:(假设 graphic
在某处初始化)
strcat(graphic, buf); /* Use strncat for limited number of characters */
还要确保 string.h
包含在您的程序中。
要自己附加字符,您可以仅使用赋值而不是使用函数。
/* Append one character (ith character of buf[i] ) */
const size_t len = strlen(graphic);
if(len + 1 >= sizeof graphics) { /* Error handling */ }
else {
graphic[len] = buf[i];
graphic[len + 1] = '\0';
}
关于c - 传递 ‘strcat’ 的参数 2 使指针来自整数而不进行强制转换 [-Wint-conversion],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36741825/