Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C, is there a difference between a NULL pointer and a pointer that points to 0? If so, what?

In C, what is the difference between a NULL pointer and a pointer that points to 0?

like image 200
user950891 Avatar asked Sep 30 '11 16:09

user950891


People also ask

IS null pointer the same as 0?

A null pointer constant is an integer constant expression that evaluates to zero. For example, a null pointer constant can be 0, 0L , or such an expression that can be cast to type (void *)0 .

What is the difference between null and 0 in C?

Null is a built-in constant that has a value of zero. It is the same as the character 0 used to terminate strings in C.

What is the difference between pointer and null pointer?

The null pointer is basically used in a program to assign the value 0 to a pointer variable of any data type. The void pointer, on the other hand, has no value assigned to it and we use it to store the addresses of other variables in the program- irrespective of their data types.

Can pointers point to zero?

A pointer can also be initialized to null using any integer constant expression that evaluates to 0, for example char *a=0; . Such a pointer is a null pointer. It does not point to any object.


2 Answers

The ISO/IEC 9899:TC2 states in 6.3.2.3 Pointers

3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) 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

The macro NULL expands to an implementation-defined null pointer constant.

Any two null pointers shall compare equal.

like image 134
Sadique Avatar answered Sep 21 '22 23:09

Sadique


Yes there is. The standard dictates that NULL always points to invalid memory. But it does not state that the integer representation of the pointer must be 0. I've never come across an implementation for which NULL was other than 0, but that is not mandated by the standard.

Note that assigning the literal 0 to a pointer does not mean that the pointer assumes the integer representation of 0. It means that the special null pointer value is assigned to the pointer variable.

like image 24
David Heffernan Avatar answered Sep 21 '22 23:09

David Heffernan