Null pointer and void pointer in C language


Null pointer and void pointer in C language
Shuimanting520 2016-05-10 07:12:12 18926 collection 30
Category column: C / C + + article tag: C + + pointer void null
Null pointer null
In C language, if a pointer does not point to any data, we call it a null pointer, which is represented by null. For example:

int *p = NULL;

Note that it's case sensitive. Null has no special meaning. It's just a common identifier.

Null is a macro definition. In stdio. H, it is defined as:

define NULL ((void *)0)

(void )0 means to cast the value 0 to voidType, the outermost () encloses the content of macro definition, which is also recommended when we define macro ourselves to prevent ambiguity.

In short, the value of P is 0. You can output the value of P:

#include <stdio.h>
int main(){
    int *p = NULL;
    printf("%d\n", p);
    return 0;

Results of operation:

We know that once a variable is defined, memory is allocated, and so is a pointer variable. For example:

int *p; // It is not a null pointer

Its value is random and garbage value. If it is used carelessly, it will cause segment error, cause the program to exit, and even modify the data unconsciously.

After defining P, it must allocate 4 bytes (32-bit environment) of space in memory, but its value is random, unlike int which will be initialized to 0, but it does point to a section of normal memory. When using P, the operation is the data of this memory. Fortunately, it can run normally, but in most cases, this memory has no right to operate.

Null causes p to point to address 0. Most systems use 0 as an unused address, so using P will not destroy the data.

However, this is not always the case. Some systems use the address 0 and define null as other values. Therefore, do not equate null with 0. The following writing is unprofessional:

int *p = 0;

It should be written as:

int *p = NULL;

Note the difference between null and nul: null represents null pointer, which is a macro definition and can be used directly in code. Nul represents the character '\ 0', which is the end of the string flag. It is the 0 th character in the ASCII code table. Nul is not defined in C language, it's just a name for '\ 0', and can't be used directly in code.

Void pointer
There is also a kind of void pointer type in C language, that is, a pointer variable can be defined, but it does not indicate which type of data it points to. For example:

void *p = malloc(2);

Allocate 2 bytes of space in memory, but not sure what type of data it holds.

Note that void pointer is different from null pointer: null indicates that the pointer does not point to any data and is "null"; The void pointer actually points to a piece of memory, but it doesn't know what kind of data is in the memory.

Recommended Today

The use of springboot Ajax

Ajax overview What is Ajax? data Ajax application scenarios? project Commodity system. Evaluation system. Map system. ….. Ajax can only send and retrieve the necessary data to the server, and use JavaScript to process the response from the server on the client side. data But Ajax technology also has disadvantages, the biggest disadvantage is that […]