简体   繁体   English

传递 'strcpy' 的参数 2 使指针来自 integer 而无需强制转换 [-Wint-conversion]

[英]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"};

Undefined Behaviour:未定义的行为:

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.否则,行为未定义。

  • Once the abstract state machine reaches an undefined state, no further assumption about the continuation of the execution of the program can be made.一旦抽象 state 机器达到未定义的 state,就无法进一步假设程序继续执行。

Re: Why can't I use strcpy in this case?回复:为什么我不能在这种情况下使用 strcpy?

Invalid arguments to strcpy :无效的 arguments 到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.

相关问题 我需要传递'strcpy'参数2的帮助,使指针从整数开始而没有强制转换[-Wint-conversion] - i need help passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion] 警告:传递'strcpy'的参数2使得整数指针没有强制转换[-Wint-conversion] | - warning: passing argument 2 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]| strcmp 错误:传递“strcmp”的参数 2 使指针从整数而不进行强制转换 [-Wint-conversion] - strcmp error: passing argument 2 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion] 传递 'strcmp' 的参数 1 和 2 使指针从整数而不进行强制转换 [-Wint-conversion] - passing argument 1 and 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion] 赋值使指针从整数变成整数,而不进行强制转换[-Wint-conversion] - assignment makes integer from pointer without a cast [-Wint-conversion] 警告:传递'pushPost'的参数1使指针从整数开始,而在我的后缀中没有强制转换[-Wint-conversion] - warning: passing argument 1 of 'pushPost' makes pointer from integer without a cast [-Wint-conversion] in my infix to postfix progam 使用 function 指针,但得到警告使来自 integer 的指针没有强制转换 [-Wint-conversion] - Using function pointer, but got warning makes pointer from integer without a cast [-Wint-conversion] 警告:赋值使 integer 从没有强制转换的指针 [-Wint-conversion] 在 C - warning: assignment makes integer from pointer without a cast [-Wint-conversion] in C 从 'char *' 对 'char' 的赋值使指针从没有强制转换的整数 [-Wint-conversion] - assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion] 为什么这是“从‘int’到‘char *’的赋值使来自 integer 的指针没有转换 [-Wint-conversion]” - Why this is "assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM