函数指针和回调函数实例1

来源:互联网 发布:西安 长安 知乎 编辑:程序博客网 时间:2024/04/30 12:23

1.common.h


#ifndef _COMMON_H_
#define _COMMON_H_


#include <stdio.h>


typedef int (*cmp)(void *, void *);
typedef struct book *Book;


struct book
{
int id;
char name[10];
};


void *max(void *array[], int len, cmp func);
void *min(void *array[], int len, cmp func);


int cmp_int(void *, void *);
int cmp_struct(void *, void *);


int insert_int(int **, int val);
int insert_struct(Book *, int, char *);


#endif




2.insert.c


#include <memory.h>
#include "common.h"


int insert_int(int **pos, int val)
{
int *p = NULL;
p = (int *)malloc(sizeof(int));
if (NULL == p)
{   
printf("Can not malloc int memeory \n");
return -1;
}
*p = val;
*pos = p;


return 0;
}


int insert_struct(Book *pos, int id, char *name)
{
Book p = NULL;
p = (Book)malloc(sizeof(struct book));
if (NULL == p)
{
printf("Can not malloc struct book memory \n");
return -1;
}
p->id = id;
strcpy(p->name, name);
*pos = p;


return 0;
}



3.cmp.c


#include "common.h"


int cmp_int(void *p, void *q)
{
int *a;
int *b;
a = (int *)p;
b = (int *)q;
if(*a > *b)
{
return 1;
}
else if(*a == *b)
{
return 0;
}
else if(*a < *b)
{
return -1;
}
    return 0;
}


int cmp_struct(void *p, void *q)
{
Book a;
Book b;
a = (Book)p;
b = (Book)q;
if(a->id > b->id)
{
return 1;
}
else if(a->id == b->id)
{
return 0;
}
else if(a->id < b->id)
{
return -1;
}
return 0;
}


4.max.c


#include "common.h"


void *max(void *array[], int len, cmp func)
{
int i = 0;
void *tmp;
tmp = array[0];
for (i = 1; i < len; i++)
{
if(-1 == (*func)(tmp, array[i]))
{
tmp = array[i];
}
}
return tmp;
}


5.min.c


#include "common.h"


void *min(void *array[], int len, cmp func)
{
int i = 0;
void *tmp;
tmp = array[0];
for (i = 1; i < len; i++)
{
if(1 == (*func)(tmp, array[i]))
{
tmp = array[i];
}
}
return tmp;
}


6.main.c


#include "common.h"


#define NUMBER 3


void main()
{
int i = 0;


Book myBook[NUMBER];
int *myIntArray[NUMBER];


Book ret1 = NULL;
int *ret2 = NULL;
    
int inputId = 0;
char inputName[10] = {'\0'};
int inputVal = 0;


for(i = 0; i < NUMBER; i++)
{
printf("Input struct book member: id name \n");
scanf("%d %s", &inputId, &inputName[0]);
if (-1 == insert_struct(&myBook[i], inputId, inputName))
{
printf("Insert_struct myBook[%d] error !\n", i);
}


printf("Input int array member: val \n");
scanf("%d", &inputVal);
if (-1 == insert_int(&myIntArray[i], inputVal))
{
printf("Insert_int myIntArray[%d] error ! \n", i);
}
}
    
printf("Insert_struct myBook is : \n");
for(i = 0; i < NUMBER; i++)
{
printf("%d %s \n", myBook[i]->id, myBook[i]->name);
}


printf("Insert_int myIntArray is : \n");
for(i = 0; i < NUMBER; i++)
{
printf("%d \n", *myIntArray[i]);
}


ret1 = (Book)max((void **)&myBook[0], NUMBER, cmp_struct);
ret2 = (int *)max((void **)&myIntArray[0], NUMBER, cmp_int);


printf("max id in myBook[3] is %d \n", ret1->id);
printf("max val in myIntArray[3] is %d \n", *ret2);

ret1 = (Book)min((void **)&myBook[0], NUMBER, cmp_struct);
ret2 = (int *)min((void **)&myIntArray[0], NUMBER, cmp_int);


printf("min id in myBook[3] is %d \n", ret1->id);
printf("min val in myIntArray[3] is %d \n", *ret2);


}


7.Makefile



CC = mips-linux-gnu-gcc
CFLAG = -EL
SRC = insert.c max.c min.c cmp.c main.c
TAG = pointer_callback


$(TAG):$(SRC)
$(CC) $(CFLAG) $(SRC) -o $(TAG)


clean:
rm -f $(TAG) 
rm -f *.o