#include<stdio.h>
#include<curses.h>
#include<string.h>
void main()
{ const char *str1[20];
const char *str2[20];
int comp;
printf("Enter the first string:\n");
scanf("%s",&str1);
printf("Enter the second string:\n");
scanf("%s",&str2);
comp=strcmp(str1,str2);
}
这是在 gcc 编译器 4.8 中编译的。详细的解释将不胜感激。
错误信息:
strcmp.c: In function ‘main’:
strcmp.c:10:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const char * (*)[20]’ [-Wformat=]
scanf("%s",&str1);
^
strcmp.c:12:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const char * (*)[20]’ [-Wformat=]
scanf("%s",&str2);
^
strcmp.c:13:14: warning: passing argument 1 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
comp=strcmp(str1,str2);
^~~~
In file included from strcmp.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘const char **’
extern int strcmp (const char *__s1, const char *__s2)
^~~~~~
strcmp.c:13:19: warning: passing argument 2 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
comp=strcmp(str1,str2);
^~~~
In file included from strcmp.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘const char **’
extern int strcmp (const char *__s1, const char *__s2)
const char *str1[20];
应该是
char str1[20];
你想要一个可变的字符数组,而不是一个指针数组。
scanf("%s",&str1);
应该是:
scanf("%s",str1);
scanf
函数需要地址来存储输入。这等同于 &str1[0]
,它是 str1
衰减成的。
我是一名优秀的程序员,十分优秀!