二级指针
# include <stdio.h>
int main(void)
{
int i = 10;
int *p = &i;
int **q = &p;
printf("i = %d\n", i);
printf("i = %d\n", *p);
printf("i = %d\n", **q);
return 0;
}
函数指针
# include <stdio.h>
int Max(int, int); //函数声明
int main(void)
{
int(*p)(int, int); //定义一个函数指针
int a, b, c;
p = Max; //把函数Max赋给指针变量p, 使p指向Max函数
printf("please enter a and b:");
scanf("%d%d", &a, &b);
c = (*p)(a, b); //通过函数指针调用Max函数
printf("a = %d\nb = %d\nmax = %d\n", a, b, c);
return 0;
}
int Max(int x, int y) //定义Max函数
{
int z;
if (x > y)
{
z = x;
}
else
{
z = y;
}
return z;
}
数组和链表
访问数组
Event *event_cur = *(calendar->events+day-1);
访问链表
event_temp = event_cur->next;
while (event_temp != NULL) {
if((*(calendar->comp_func))(event_temp, event)<0){
event_cur = event_temp;
event_temp = event_temp->next;
}
else{
break;
}
}
event_cur->next = event;
event->next = event_temp;
/**************************************************/
event_temp = event_cur->next;
while (event_temp != NULL) {
if(strcmp(event_temp->name,name) == 0){
event_cur->next = event_temp->next;
if(event_temp->info != NULL&&calendar->free_info_func != NULL){
(*(calendar->free_info_func))(event_temp->info);
}
free(event_temp->name);
free(event_temp);
calendar->total_events -= 1;
event_temp = event_cur->next;
}
else{
event_cur = event_cur->next;
event_temp = event_temp->next;
}
}
int add_event(Calendar *calendar, const char *name, int start_time, int duration_minutes, void *info, int day){
Event *event;
Event *event_cur = *(calendar->events+day-1);
Event *event_temp = event_cur;
if(calendar==NULL||name==NULL||start_time<0||start_time>2400||duration_minutes<=0||day<1||day>calendar->days){
return FAILURE;
}
/*check event existing*/
if(find_event(calendar, name,&event)==SUCCESS){
return FAILURE;
}
event = (Event *)malloc(sizeof(Event));
event->name = (char *)malloc(80*sizeof(char));
strcpy(event->name, name);
event->start_time = start_time;
event->duration_minutes = duration_minutes;
event->info = info;
event->next = NULL;
if(event == NULL||event->name ==NULL){
return FAILURE;
}
/* insert event */
if(event_cur==NULL){
*(calendar->events+day-1) = event;
}
else if((*(calendar->comp_func))(event_cur, event)>0){
*(calendar->events+day-1) = event;
event->next = event_cur;
}
else{
event_temp = event_cur->next;
while (event_temp != NULL) {
if((*(calendar->comp_func))(event_temp, event)<0){
event_cur = event_temp;
event_temp = event_temp->next;
}
else{
break;
}
}
event_cur->next = event;
event->next = event_temp;
}
calendar->total_events += 1;
return SUCCESS;
Makefile
CC = gcc
CFLAGS = -ansi -Wall -g -O0 -Wwrite-strings -Wshadow \
-pedantic-errors -fstack-protector-all
PROGS = public01 public02 public03 public04 public05 student_tests
.PHONY: all clean
all: $(PROGS)
clean:
rm -f *.o $(PROGS) a.out
$(PROGS): calendar.o my_memory_checker_216.o
public01: public01.o
public02: public02.o
public03: public03.o
public04: public04.o
public05: public05.o
student_tests: student_tests.o
calendar.o: calendar.h event.h
my_memory_checker_216.o: my_memory_checker_216.h
public01.o: calendar.h my_memory_checker_216.h event.h
public02.o: calendar.h my_memory_checker_216.h event.h
public03.o: calendar.h my_memory_checker_216.h event.h
public04.o: calendar.h my_memory_checker_216.h event.h
public05.o: calendar.h my_memory_checker_216.h event.h
student_tests.o: calendar.h my_memory_checker_216.h event.h
sponsor:
@echo "This exercise brought to you by pepsi"