当前位置: C语言 -- 标准库 -- <string.h> -- NULL

NULL宏


概要:

#define NULL value //value值由具体实现定义。


描述:

该宏扩展为实现定义的空指针常量,可能具有以下形式:

1、值为0的整型常量表达式。

2、转换为(void *)类型的,值为0的整型常量表达式。

空指针常量能够转换为任何指针类型,这种转换能够得到对应类型的空指针值。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
/*宏NULL范例*/

#include <stdio.h>
#include <string.h>

int main(void)
{
    char strOne[] = "All for one, one for all.";
    const char strTwo[] = ", .";
    char *ptr;
    int count = 0;

    /*统计句子中的单词数。*/
    ptr = strtok(strOne, strTwo);
    while(ptr != NULL)
    {
        ++count;
        ptr = strtok(NULL, strTwo);
    }

    printf("Total %d word%s.\n", count, (count>1)?"s":"");

    return 0;
}


输出:

Total 6 words.