assert() 宏用法

assert() 宏用法
注意:assert是宏,而不是函数。在C的assert.h头文件中。

#inlcude <assert.h>

用法:
void assert( int expression );
assert的作用是先计算表达式expression,如果其值为假(即为0),那么它先向标准错误流stderr打印一条出错信息,然后通过调用abort来终止程序运行;否则,assert()无任何作用。

例子:

void CLinkList::reverse()
{
PBINODE root = getroot();
assert(root != NULL);

PBINODE prenode = root->pnext;
assert(prenode != NULL);

PBINODE curnode = prenode->pnext;
prenode->pnext = NULL;
while (curnode != NULL)
{
PBINODE nextnode = curnode->pnext;
curnode->pnext = prenode;
prenode = curnode;
curnode = nextnode;
}

root->pnext = prenode;
}

posted @ 2015-01-21 13:54  披头  阅读(257)  评论(0)    收藏  举报