| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 618 人关注过本帖
标题:怎么extern void * max()不能通过?
取消只看楼主 加入收藏
aifen
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-12-14
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
怎么extern void * max()不能通过?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef int (*cmp)(void *,void *);
typedef struct book* Book;
struct book
{
        int id;
        char name[10];
};
extern void *max(void *array[],int len,cmp func);
extern int cmp_int(void *p,void *q);
extern int cmp_struct(void *p,void *q);
extern int insert_struct(Book *pos,int id,char *name);
extern int insert_int(int **pos,int val);

————————————————————————————————————
#include "common.h"
void *max(void *array[],int len,cmp func)
{
        int i;
        void tmp;
        tmp=array[0];
        for(i=1;i<len;i++)
        {
                if((*func)(tmp,array[i])==-1)
                        tmp=array[i];
        }
        return tmp;
}
——————————————————————————————————————
#include "common.h"
int insert_struct(Book *pos,int id,char *name)
{
        Book p;
        p=(Book)malloc(sizeof(struct book));
        if(p==NULL)
        {
                printf("fail to malloc");
                return -1;
        }
        p->id=id;
        strcpy(p->name,name);
        *pos=p;
        return 0;
}
int insert_int(int **pos,int val)
{
        int *p;
        p=(int *)malloc(sizeof(int));
        *p=val;
        *pos=p;
        return 0;
}
————————————————————————
#include "common.h"
#define MAX 3

int main(void)
{
        Book array1[MAX];
        int *array[MAX];
        int i;        int id;        int val;
        char name[10];
        Book res1;
        int *res2;
        for(i=0;i<MAX;i++)
        {
                printf("input info of book \n");
                scanf("%d",&id);
                scanf("%s",name);
                if(insert_struct(array1+i,id,name)==-1)
                        exit(1);
                printf("input int \n");
                scanf("%d",&val);
                if(insert_int(array+i,val)==-1)
                        exit(1);
        }
        res1=(Book)max((void **)array1,MAX,cmp_struct);
        res2=(int *)max((void**)array,MAX,cmp_int);
        printf("the max of books:%d,%s\n",res1->id,res1->name);
        printf("the max of int:%d \n",*res2);
        return 0;
}

其中
extern void *max(void *array[],int len,cmp func);
不能通过

[此贴子已经被作者于2016-12-14 09:47编辑过]

2016-12-14 09:45
快速回复:怎么extern void * max()不能通过?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019648 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved