**
1
**.
assert.h防御式编程
assertion断言
下面所述的在一定情况下为非预期错误的一些例子:
(1)空指针。
(2)输入或者输出参数的值不在预期范围内。
(3)数组的越界。
assert(date1 == NULL);//进行断言
执行 Assert(1 == 0, “Error”); 结果为:
Assertion failed: 1 == 0
Message: Error
**
2
**
You can have “namespaces” in C (the Apache Portable Runtime prefixes all globals symbols with apr_ and GLib prefixes them with g_ to create a namespace) and other organizing factors without OOP.
“If you need stuff like overloading and/or virtual methods, you probably need to include function pointers in structures”
typedef struct ShapeClass ShapeClass;
struct ShapeClass{
float (*computeArea)(const ShapeClass *shape);
} ;
float shape_computeArea(const ShapeClass *shape)
{
return shape->computeArea(shape);
}
This would let you implement a class, by “inheriting” the base class, and implementing a suitable function:
typedef struct {
ShapeClass shape;
float width, height;
} RectangleClass;
static float rectangle_computeArea(const ShapeClass *shape)
{
const RectangleClass *rect = (const RectangleClass *) shape;
return rect->width * rect->height;
}
void rectangle_new_with_lengths(RectangleClass *rect, float width, float height)
{
rectangle_new(rect);
rect