This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.
Released under the GNU General Public License.
NULL
Prototype
#include <stddef.h>
#define NULL implementation-defined
Description
The NULL
macro expands to an implementation-defined
null pointer constant.
It is usually defined as a void
pointer or a zero value, like:
#define NULL ((void *)0)
or
#define NULL 0
The NULL
pointer constant should not be confused with the string
terminating null character '\0'
, witch has another mean.
For example, for finding the end of a string, we use
the terminating null character:
while (*s != '\0')
++s;
while for checking for a null pointer, we use the NULL constant:
char *p;
p = strchr("abcde", 'x');
if (p == NULL)
printf("Character not found\n");
Example
#include <stdio.h>
#include <stdlib.h>
/*
* Get the next token taken from the `sep' separated argument
* string `s'.
* Return NULL when there are no more tokens available.
*/
char *next_token(char *buf, const char **s, int sep)
{
char *dp = buf;
while (**s == sep)
++*s;
if (**s == '\0')
return NULL;
while (**s != sep && **s != '\0')
*dp++ = *(*s)++;
*dp = '\0';
return buf;
}
/*
* Print the tokens in witch is composed the colon `:' separated
* argument string.
*/
void print_tokens(const char *s)
{
char buf[32];
while (next_token(buf, &s, ':') != NULL)
printf("token = `%s'\n", buf);
}
int main(void)
{
print_tokens("/bin:/sbin:/usr/bin:/usr/sbin:.");
return EXIT_SUCCESS;
}
References
ISO C 9899:1990 7.1.6