其实原因也很简单,当令需要某个类型来声明变量时,不需分配内存,不需要对其进行操作,自然就不用了解其具体的类型定义。但当你使用时,要分配内存时,就必须要了解类型是怎么定义的,否则这些操作无法完成,这自然就需要知道类型的具体定义。
其实,在头文件中仅声明类型的目的是为了信息隐藏,也就是不让调用者知道这个类型具体的定义是什么样子的,那么就需要像Java/C++(www.cppentry.com)中那样去定义这个类型,
1)把类型声明为指针类型:
typedef struct Point *Point;
否则调用者还是有可能去定义。
2)也即在头文件的对应源文件中封装操作此类型的所有方法,这样外界就没有必要去了解类型是如何定义的了。它想操作时,仅需要调用封装的方法即可。
典型的实例:
头文件point.h:
#ifndef __POINT_H
#define __POINT_H
typedef struct _Point *Point;
Point make_point();
void print_point(Point point);
void destroy_point(Point p);
#endif
实现源文件:point.c
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "point.h"
struct _Point {
int x;
int y;
};
Point make_point() {
Point point = (Point) calloc(1, sizeof(struct _Point));
point->x = 0;
point->y = 0;
return point;
}
void print_point(Point point) {
printf("point %d, %d\n", point->x, point->y);
}
void destroy_point(Point p) {
if (p == NULL) {
printf("warning, destroying NULL object");
return;
}
free(p);
}