我正在尝试扫描一个字符串并将其放入函数中,但是它不起作用

最初它工作正常

int function(char *pointer, char *pointer0){
    if(pointer[0]==pointer0[0]){
        printf("Same");
    }
    else{
        printf("Not same");
    }
}

int main(){
    char string;
    function("example","echinodermata");
    return 0;
    }

但是,当我尝试将其转换为需要用户输入的内容时,它无法正常工作,并且我不明白为什么,该如何解决?

int function(char *pointer, char *pointer0){
    printf("%s %s\n",pointer,pointer0);
    if(pointer[0]==pointer0[0]){
        printf("Same");
    }
    else{
        printf("Not same");
    }
}

int main(){
    char string,string0;
    scanf("%s",&string);
    scanf("%s",&string0);
    function(&string,&string0);
    return 0;
    }
评论
  • Burne
    Burne 回复

    You've made a mistake in this case. You haven't defined any array length for string and string0 variables which are declared as a single char. If you do something like:

    char string[10], string0[10]; // array length defined
    

    Secondly, you had used the placeholder %s with char data-type only, so you must use array for accepting multiple characters.

    会的