format '%s' expects argument of type 'char *'

后端 未结 7 473
野趣味
野趣味 2021-01-11 10:32
#include 
int main(void)
{
    int i,j,k;
    char st;
    printf(\"enter string\\n\");
    scanf(\"%s\", st);         


        
相关标签:
7条回答
  • 2021-01-11 10:44

    char st is a single character. Judging by the rest of your code, you probably intended to declare an array of characters:

    char st[80];
    
    0 讨论(0)
  • 2021-01-11 10:49

    As others have mentioned you want to create an array:

    Change char st; to char st[10]; or whatever size array you want.

    With the above change st is an array that has 10 individual elements that can hold a single char value.

    0 讨论(0)
  • 2021-01-11 10:53

    You have a type mis-match.
    scanfis not type safe, You need to provide proper type. scanf enables you to get data from the input and you need to tell it what is the type of the data you want it to read. You ask it to read a string by specifying %s by provide it with a character variable.

    You need an array:

    #define MAX_LENGTH 256
    char st[MAX_LENGTH];
    

    As @Jerry rightly points out, You can just simple avoid all the hassle by using:
    getline() instead of using scanf

    0 讨论(0)
  • 2021-01-11 10:58

    char st is a character i.e it will store only one character try it like this main() char st [100]; scanf("%s",st); just change these three lines and it will work it will except only single word strings here you have declared the function as an int that is int main() keep it as main() and it will work

    0 讨论(0)
  • 2021-01-11 10:59

    st is type of char &st is type of char * Take care of the difference. BTW, only one char cannot be used to store a string. Use char[] array.

    0 讨论(0)
  • 2021-01-11 11:03

    Use char *st; or an array like char st[50];.

    When you complete the usage of a char pointer, you should deallocate the memory used by the pointer. This can be done using free(st); function.

    EDIT : As you are printing string and if you are using pointer, you can do:

    printf("the entered string is %s\n",st);
    printf("the entered string is %s\n",*st); // This will work in both cases, if you use char *st or char st[50]
    
    0 讨论(0)
提交回复
热议问题