C语言程序对的但是编译错误
好像是排序的函数comp有问题,程序是求到a和b数组的并集c并对数组c排序,程序是对的。编译错误的信息:2548038.69510/Main.cc:Infunction‘...
好像是排序的函数comp有问题,程序是求到a和b数组的并集c并对数组c排序,程序是对的。
编译错误的信息:2548038.69510/Main.cc: In function ‘int main()’:
2548038.69510/Main.cc:15:26: error: invalid conversion from ‘void*’ to ‘int*’
2548038.69510/Main.cc:16:26: error: invalid conversion from ‘void*’ to ‘int*’
2548038.69510/Main.cc:28:33: error: invalid conversion from ‘void*’ to ‘int*’
我把(const void*a,const void*b)改成(const int*a,const int*b)后又多了两排错误
2548582.70054/Main.cc:63:30: error: invalid conversion from ‘int (*)(const int*, const int*)’ to ‘int (*)(const void*, const void*)’
2548582.70054/Main.cc:63:30: error: initializing argument 4 of ‘void qsort(void*, size_t, size_t, int (*)(const void*, const void*))’
代码:
#include <stdio.h>
#include <stdlib.h>
int comp(const void*a,const void*b)
{
return *(int*)a-*(int*)b;
}
int main()
{
int *a,*b,*c;
int n,m,sum;
int i,j,flag;
scanf("%d %d",&n,&m);
a = malloc(n*sizeof(int));
b = malloc(m*sizeof(int));
for(i = 0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i = 0;i<m;i++)
{
scanf("%d",&b[i]);
}
c = malloc((n+m)*sizeof(int));
for(i = 0;i<n;i++)
{
c[i] = a[i];
}
sum = n;
for(i = 0;i<m;i++)
{
flag = 0;
for(j = 0;j<n;j++)
{
if(a[j]==b[i])
flag = 1;
}
if(flag!=1)
{
c[sum] = b[i];
sum++;
}
}
printf("%d\n",sum);
qsort(c,sum,sizeof(int),comp);
for(i=0;i<sum;i++){
printf("%d ",c[i]);
}
return 0;
} 展开
编译错误的信息:2548038.69510/Main.cc: In function ‘int main()’:
2548038.69510/Main.cc:15:26: error: invalid conversion from ‘void*’ to ‘int*’
2548038.69510/Main.cc:16:26: error: invalid conversion from ‘void*’ to ‘int*’
2548038.69510/Main.cc:28:33: error: invalid conversion from ‘void*’ to ‘int*’
我把(const void*a,const void*b)改成(const int*a,const int*b)后又多了两排错误
2548582.70054/Main.cc:63:30: error: invalid conversion from ‘int (*)(const int*, const int*)’ to ‘int (*)(const void*, const void*)’
2548582.70054/Main.cc:63:30: error: initializing argument 4 of ‘void qsort(void*, size_t, size_t, int (*)(const void*, const void*))’
代码:
#include <stdio.h>
#include <stdlib.h>
int comp(const void*a,const void*b)
{
return *(int*)a-*(int*)b;
}
int main()
{
int *a,*b,*c;
int n,m,sum;
int i,j,flag;
scanf("%d %d",&n,&m);
a = malloc(n*sizeof(int));
b = malloc(m*sizeof(int));
for(i = 0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i = 0;i<m;i++)
{
scanf("%d",&b[i]);
}
c = malloc((n+m)*sizeof(int));
for(i = 0;i<n;i++)
{
c[i] = a[i];
}
sum = n;
for(i = 0;i<m;i++)
{
flag = 0;
for(j = 0;j<n;j++)
{
if(a[j]==b[i])
flag = 1;
}
if(flag!=1)
{
c[sum] = b[i];
sum++;
}
}
printf("%d\n",sum);
qsort(c,sum,sizeof(int),comp);
for(i=0;i<sum;i++){
printf("%d ",c[i]);
}
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询