简体   繁体   中英

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(newContact->fieldsValue[i], getString(newContact->fieldsValue[i], 30, listFieldsName[i]));

i want to get value of 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:

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; 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.

Re: Why can't I use strcpy in this case?

Invalid arguments to strcpy :

From C11:

The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by 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.

Answer: getstring returns a char , not a char * . You're passing a char in place of a char * to strcpy .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM