一道compile error题 (POJ 1002)

bakerzhu 2014-08-12 11:48:04
模仿这个写了一个程序。有两个问题:1.示例程序OJ AC, 自己写的那个Compile error; 2. 两个程序在Dev-C++ 5.6.1中都不能成功编译,原因均为:

[Error] invalid conversion from 'int (__attribute__((__cdecl__)) *)(const char*, const char*)' to 'int (*)(const void*, const void*)' [-fpermissive]

下面是示例程序。
//AC. Copyright Derek Kisman (ACM ICPC ECNA 1999)
//此为示例程序
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char map[26] = {'2', '2', '2', '3', '3', '3', '4', '4', '4',
'5', '5', '5', '6', '6', '6', '7', 0, '7',
'7', '8', '8', '8', '9', '9', '9', 0};
char ph[100000][9];
int nph;
char buf[1000];
int main() {
int i, j, k, x, y, z, n;
char ch;
memset( ph, 0, sizeof(ph) );
scanf( " %d", &nph );
for( i = 0; i < nph; i++ ) {
scanf( " %s", buf );
x = 0;
for( j = 0; buf[j]; j++ ) {
if( buf[j] == '-' ) continue;
if( buf[j] >= 'A' && buf[j] <= 'Z' ) buf[j] = map[buf[j]-'A'];
ph[i][x++] = buf[j];
if( x == 3 ) ph[i][x++] = '-';
}
}
qsort( ph, nph, 9, strcmp );
x = 1; z = 0;
for( i = 1; i < nph; i++ ) {
if( strcmp( ph[i-1], ph[i] ) ) {
if( x > 1 ) {
printf( "%s %d\n", ph[i-1], x );
z = 1;
}
x = 1;
}
else x++;
}
if( x > 1 ) {
printf( "%s %d\n", ph[i-1], x );
z = 1;
}
if( !z ) printf( "No duplicates.\n" );
}

下面是自己写的程序

//自己写的, OJ Compile error
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char ph[100000][9],buf[1000];
char map[26]={'2','2','2','3','3','3','4','4','4','5','5','5','6',
'6','6','7',0,'7','7','8','8','8','9','9','9',0};
int nph;
int main(){
int i,j;
memset(ph,0,sizeof(ph));
scanf(" %d",&nph);
for(i=0;i<nph;i++){
scanf(" %s",&buf);
int x=0;
for(j=0;buf[j];j++){
if(buf[j]=='-') continue;
if((buf[j]>='A'&&buf[j]<'Z')||
(buf[j]>=0&&buf[j]<=9))
buf[j]=map[buf[j]-'A'];
ph[i][x++]=buf[j];
if(x==3) ph[i][x++]='-';
}
}
qsort(ph,nph,9,strcmp);
int x=1,z=0;
for(i=1;i<nph;++i){
if(strcmp(ph[i-1],ph[i])){
if(x>1) {
printf("%s %d\n",ph[i-1],x);
z=1;
}
x=1;
}
else x++;
}
if(x>1){
printf("%s %d\n",ph[i-1],x);
z=1;
}
if(!z) printf("No duplicates.\n");
}
...全文
313 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bakerzhu 2014-08-19
  • 打赏
  • 举报
回复
引用 8 楼 lovesmiles 的回复:
typedef int (*func)(const void*, const void*); qsort(ph,nph,9,(func)strcmp);//强制类型转换
在CPP中编译成功了。谢谢!
bakerzhu 2014-08-19
  • 打赏
  • 举报
回复
引用 6 楼 gojoy_x13 的回复:
这个不知道,可以跟一下头文件,觉得像是dev-c的头文件有问题。第一个在dev-c里也能编过吗
两个在devcpp里都编译不通过。但是示例程序能AC
gojoy_x13 2014-08-13
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char ph[100000][9], buf[1000];
char map[26]={'2','2','2','3','3','3','4','4','4','5','5','5','6',
	'6','6','7',0,'7','7','8','8','8','9','9','9',0};

int nph;
int main(){
	int i, j;
	int x=1,z=0;
	memset(ph, 0, sizeof(ph));
	scanf("%d", &nph);

	for(i=0; i<nph; i++){
		int x = 0;
		scanf("%s", buf);

		for(j=0; buf[j]; j++) {
			if(buf[j]=='-') continue;
			if((buf[j]>='A'&&buf[j]<'Z')||
				(buf[j]>=0&&buf[j]<=9))
				buf[j]=map[buf[j]-'A'];
			ph[i][x++]=buf[j];
			if(x==3) ph[i][x++]='-';
		}
	}
	qsort(ph,nph,9, strcmp);

	x = 1;
	z = 0;

	for(i=1;i<nph;++i){
		if(strcmp(ph[i-1],ph[i])){
			if(x>1)    {   
				printf("%s %d\n",ph[i-1],x);
				z=1;
			}
			x=1;
		}
		else x++;
	}
	if(x>1){
		printf("%s %d\n",ph[i-1],x);
		z=1;
	}
	if(!z)  printf("No duplicates.\n");
}
具体代码意思不懂,c里面的变量申明放在 代码块首。
brookmill 2014-08-13
  • 打赏
  • 举报
回复
scanf(" %s",&buf); // 这里不应该用& 用gcc编译通过,不过关于qsort和strcmp有个warning
brookmill 2014-08-13
  • 打赏
  • 举报
回复
引用 1 楼 brookmill 的回复:
#include string.h要放到stdlib.h前面
不对,不是这个问题,刚才糊涂了
勤奋的小游侠 2014-08-13
  • 打赏
  • 举报
回复
typedef int (*func)(const void*, const void*); qsort(ph,nph,9(,func)strcmp);//强制类型转换
brookmill 2014-08-13
  • 打赏
  • 举报
回复
应该是qsort参数类型的问题。qsort函数声明里面,最后一个参数应该是int (*)(const void*, const void*),但是strcmp是int (__attribute__((__cdecl__)) *)(const char*, const char*) 用gcc编译有个警告,Dev-cpp 5.6.1就不知道了。
gojoy_x13 2014-08-13
  • 打赏
  • 举报
回复
这个不知道,可以跟一下头文件,觉得像是dev-c的头文件有问题。第一个在dev-c里也能编过吗
bakerzhu 2014-08-13
  • 打赏
  • 举报
回复
应该是scanf("%s",&buf)那一句不该加&号了。还有一个问题,就是为什么在Dev-cpp 5.6.1环境中编译会提示strcmp那一行出现下列错误?

[Error] invalid conversion from 'int (__attribute__((__cdecl__)) *)(const char*, const char*)' to 'int (*)(const void*, const void*)' [-fpermissive]
brookmill 2014-08-12
  • 打赏
  • 举报
回复
#include string.h要放到stdlib.h前面

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧