[英]passing argument 2 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]
char getString(char *str, int length, char field[20])
{
printf(" %s: ", field);
fflush(stdin);
fgets(str, length, stdin);
str[strlen(str) - 1] = '\0';
fflush(stdin);
return *str;
}
why i can't use strcpy in this case为什么在这种情况下我不能使用 strcpy
strcpy(newContact->fieldsValue[i], getString(newContact->fieldsValue[i], 30, listFieldsName[i]));
i want to get value of fieldName我想获取 fieldName 的值
struct newContact = {
char *fieldsName[30],
char *fieldsValue[30],
struct newContact* next;
}
char *listFieldsName = {"a", "b", "c"};
fflush(stdin);
Flushing stdin invokes undefined behaviour.刷新标准输入调用未定义的行为。
From C11:来自 C11:
If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file;
如果 stream 指向 output stream 或未输入最近操作的更新 stream,则 fflush function 会导致该 stream 的任何未写入数据被写入主机文件环境; otherwise, the behavior is undefined.
否则,行为未定义。
Re: Why can't I use strcpy in this case?回复:为什么我不能在这种情况下使用 strcpy?
strcpy
:strcpy
: From C11:来自 C11:
The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1.
strcpy function 将s2指向的字符串(包括终止符null)复制到s1指向的数组中。 If copying takes place between objects that overlap, the behavior is undefined.
如果复制发生在重叠的对象之间,则行为未定义。
The function strcpy
takes the source string and destination string, where a string is an array of null-terminated bytes. function
strcpy
采用源字符串和目标字符串,其中字符串是空终止字节数组。
Answer: getstring
returns a char
, not a char *
.答案:
getstring
返回一个char
,而不是char *
。 You're passing a char
in place of a char *
to strcpy
.您将
char
代替char *
传递给strcpy
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.