I know NULL is a kind of pointer which points no location in memory but let me know that where to use this (NULL) exactly. I have seen it in case of File Handling as id(fp!=NULL)
, passing it to time(time_t *second)
as time(NULL)
, sometimes like int *p=NULL
and some other cases too. I also use these several times(imitating) but I am not able to understand the exact use of this pointer or directive whatever it is. Please explain the meaning of NULL
vividly and apparently with examples. I am not telling to describe the above mentioned cases, they are just examples. Please exemplify in such a way that I may know the actual use of NULL and could find my answer of the mentioned cases.

- 43
- 4
-
NULL simply means 0. A 0th location is not a valid location on memory. fp!=NULL means the file handler returned the error code as non 0. time(NULL) means the constructor time receives a numeric argument 0 which gives curent time. int *p = NULL means the pointer is pointing nowhere in the memory. NULL means 0 nothing else – Himanshu Poddar Jul 17 '22 at 05:41
-
What is the difference to pass some other pointers and passing NULL in case of`time()` function? – Anup Adhikari Jul 17 '22 at 05:51
-
1"I know NULL is a kind of pointer ,,," --> Not quite. `NULL` is a macro which expands to a _null pointer constant_. It may be an integer with a value of 0 or some `void *`. – chux - Reinstate Monica Jul 17 '22 at 05:59
-
@AnupAdhikari check this out https://stackoverflow.com/questions/1282295/what-exactly-is-nullptr – Himanshu Poddar Jul 17 '22 at 06:01
-
`NULL` is a *well-defined* invalid pointer value, guaranteed to compare unequal to any object or function pointer. It's typically used to indicate that a pointer variable isn't currently pointing to anything, or to mark the end of a sequence (similar to the 0 terminator in a string), or to indicate an error condition. For example, in a linked list, each element of the list explicitly points to the next element in the sequence, and we typically set the last element to point to `NULL` to indicate the end of the list. `fopen` returns `NULL` to indicate it couldn't open the file. – John Bode Jul 17 '22 at 20:20
-
Imagine you're filling out some kind of form — any kind of form. Perhaps it asks you for your phone number, but you don't have a phone, so you write in "n/a" — "not applicable". `NULL` is sort of like "n/a" for pointers. On that form, you could fill in any phone number, or you could fill in the special value "n/a" to indicate that you had no phone number to fill in. In C programming, a pointer can point to absolutely anywhere in memory — or it can be the special value `NULL`, indicating that it points nowhere at all. – Steve Summit Jul 18 '22 at 11:05
3 Answers
Typically, NULL
1) is used to indicate that the pointer in not pointing to any valid memory location. Hence, you can use it to
- initialise a pointer type variable, or
- can also used as a return value from a function to indicate some error occurred, if that function is returning a pointer, or
- can also be assigned to pointer if the memory pointed by pointer is freed or pointer is no more need to hold that memory reference etc.
I have seen it in case of File Handling as id(fp!=NULL), passing it to time(time_t *second)as time(NULL) ....
In these kind of cases, you should refer the man page/description of the library functions in the context of which the NULL
is used.
For e.g. usage of NULL
here - time(NULL)
:
From time() (emphasis mine):
time_t time(time_t *tloc);
DESCRIPTION
time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).If tloc is non-NULL, the return value is also stored in the memory pointed to by tloc.
So, NULL
is supposed to be passed to time()
to indicate that there is no memory address is passed. Instead of NULL
, if a valid memory address is passed to time()
then it will also store the return value in that memory address, otherwise it will simply return a value of type time_t
.
Similarly, here - id(fp!=NULL)
(I am assuming fp
is file pointer which is of type FILE *
and holding fopen()
return value), we need to look at fopen()
library function description.
From fopen() (emphasis mine):
Return value
- If successful, returns a pointer to the new file stream. The stream is fully buffered unless filename refers to an interactive device. On error, returns a null pointer. POSIX requires that errno be set in this case.
So fp!=NULL
check is to confirm that fp
is valid pointer to the file stream associated with that file and there is no error occurred during file opening.
1). From C11#6.3.2.3p3
3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
From C11#7.19p3
3 The macros are
NULL
which expands to an implementation-defined null pointer constant; ....

- 11,654
- 2
- 15
- 32
-
```#include
#include – Anup Adhikari Jul 17 '22 at 05:49int main () { time_t seconds; seconds = time(NULL); printf("Seconds since January 1, 1970 = %ld\n", seconds); return(0); }``` You had told that NULL is supposed to be passed to time() to indicate that there is no variable address is passed and user don't want to store the value, returned by time(), in any variable. I think here it is stored in `seconds` variable and still passed NULL what does it mean? -
1@AnupAdhikari the `time()` description says - `If tloc is non-NULL, the return value is also stored in the memory pointed to by tloc.`. Read it carefully and one more time. It says `time()` return value **also** stored in the memory location passed as argument to it. – H.S. Jul 17 '22 at 05:54
NULL is used for validating whether a pointer is valid or invalid. A pointer contain the address it point so. So normally, without NULL, it is very difficult to know whether the pointer is valid or invalid. If something do with invalid address it will cause crash for the system. NULL = (void *)0 which used to be the reset address.
Here is some common usages:
- To check whether the pointer provide is valid or not.
- As return value for pointer, to notify that the pointer return is not valid due to some error.
- As a call_back pointer, it is used to check whether the callback is configured or not.

- 162
- 7
-
Can you help me with some examples? I am being confused because in this function `time_t time(time_t *second)` it is asking pointer of `time_t` type and we are passing `NULL ` to it. What does it mean and what kind a value is `time_t`? – Anup Adhikari Jul 17 '22 at 05:35
-
-
-
yes, but NULL is false positive case that able to detect. When it is different from NULL, it is difficult to know whether it is valid or invalid pointer. – ThongDT Jul 18 '22 at 01:26
-
@AnupAdhikari [For historical reasons](https://stackoverflow.com/questions/7550269/what-is-timenull-in-c/46025899#46025899), the `time` function can either return the time, *or* it can fill the current time value in via a pointer that you hand it. If you don't need it to do that, you can pass `NULL` as a "not applicable" value. You're saying, "Hey, Mr. `time`, don't bother filling in the time value via a pointer, today I just need to return it to me, and I'll get it from you that way." – Steve Summit Jul 18 '22 at 11:08
We need to know if the pointer is assigned to a value representing a region in memory or not. By convention (this is not enforced, but a best practice), if it is NULL, it isn't assigned to a valid region in memory.
By default, pointers are assigned to unknown values.
int* x; // we have no idea what the value is.
We can't do a comparison such as x == NULL
to figure out if it was assigned to, yet.
x = NULL; // now we know it isn't assigned to a memory region.

- 11,090
- 5
- 65
- 85