【发布时间】:2019-08-23 13:05:51
【问题描述】:
编写一个 C 程序,读取一系列的字符串并仅打印那些以字母“ed”结尾的字符串。
我的代码:
#include<stdio.h>
#include<string.h>
int main(){
char array[5][10];
int i;
for(i=0;i<5;i++){
printf("%s","enter a string");//enter string
scanf("%10s",&array[i][0]);
}
char *array[5][0];
for(i=0;i<=4;i++){
length=strlen(array[i][0]);//
if(strcmp((array[i][length-2],"ed")==0) //I wrote to make a comparison//
{
printf("%s\n",&array[i][0]);
}
}
return 0;
}
错误:
extern.c:14:7:conflicting types for ‘array’
char *array[5][0];
^~~~~
extern.c:6:6: note: previous declaration of ‘array’ was here
char array[5][10];
^~~~~
extern.c:16:1: error: ‘length’ undeclared (first use in this function)
length=strlen(array[i][0]);
^~~~~~
extern.c:16:1: note: each undeclared identifier is reported only once for each function it appears in
extern.c:17:11: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
if(strcmp((array[i][length-2],"ed")==0)
^
In file included from extern.c:2:0:
/usr/include/string.h:136:12: note: expected ‘const char *’ but argument is of type ‘int’
extern int strcmp (const char *__s1, const char *__s2)
^~~~~~
extern.c:17:4: error: too few arguments to function ‘strcmp’
if(strcmp((array[i][length-2],"ed")==0)
^~~~~~
In file included from extern.c:2:0:
/usr/include/string.h:136:12: note: declared here
extern int strcmp (const char *__s1, const char *__s2)
^~~~~~
【问题讨论】:
-
您已经声明了
array。第二个是不同的类型。 -
不要将地址运算符
&用于printf。编译器已经传递了地址。 -
删除第二个声明和
&,代码应该可以运行了。 -
...而且有一个
(太多在调用strcmp。 -
“'array' 的冲突类型”是不言自明的。