[英]I am trying to scanf a string and put it in a function, but it doesn't work
Initially it worked fine最初它工作得很好
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;
}
But, when i tried to convert it to take user input, it doesn't work correctly and i don't understand why, how do i fix this?但是,当我尝试将其转换为接受用户输入时,它无法正常工作,我不明白为什么,我该如何解决这个问题?
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;
}
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
.您尚未为声明为单个
char
的string
和string0
变量定义任何数组长度。 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.其次,您只使用了
char
数据类型的占位符%s
,因此您必须使用数组来接受多个字符。
It'll work.它会工作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.