C语言中编译相关的常见错误
作者:网络转载 发布时间:[ 2013/2/25 10:10:24 ] 推荐标签:
所以,如果要具体使用某个类型时,其所包含的头文件中必须要有类型的具体定义:
#ifndef __POINT_H
#define __POINT_H
typedef struct _Point Point;
struct _Point {
int x;
int y;
};
#endif
#include "point.h"
Point *n = (Point *) calloc(1, sizeof(Point));
n->x = 1;
n->y = 2;
....
其实原因也很简单,当令需要某个类型来声明变量时,不需分配内存,不需要对其进行操作,自然不用了解其具体的类型定义。但当你使用时,要分配内存时,必须要了解类型是怎么定义的,否则这些操作无法完成,这自然需要知道类型的具体定义。
其实,在头文件中仅声明类型的目的是为了信息隐藏,也是不让调用者知道这个类型具体的定义是什么样子的,那么需要像Java/C++中那样去定义这个类型,
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
", point->x, point->y);
}
void destroy_point(Point p) {
if (p == NULL) {
printf("warning, destroying NULL object");
return;
}
free(p);
}

sales@spasvo.com