This article and we share the main is the C language null pointer null and void pointers related content, together look at it, hope to learn C language to help you.empty pointer nullA pointer variable can point to any piece of memory on the computer, regardless of whether the memory is not assigned, or if it has permission to use it, as long as the address is given to it, it can point to the,c language there is no mechanism to ensure that the pointing memory is correct, and the programmer must be vigilant. It is very dangerous for many beginners to inadvertently manipulate pointers without initialization, as shown in the following example: 1. #include2. int main () {3. char * STR; 4. Gets (str); 5. printf ("%s \ n", str); 6. return 0; 7. This program has no syntax errors and can be compiled and linked, but when the user finishes typing the string and presses the ENTER key, an error occurs and the Linux is represented as a segment error ( segment fault ), Windows the program crashes directly. If you are lucky enough, or if you enter fewer strings, you may not have an error, which is unknown. As we said before, the value of uninitialized local variables is indeterminate,c language does not stipulate this, different compilers have different implementations, I have warned you not to use the uninitialized local variables directly. In the above code,, str is an uninitialized local variable whose value is indeterminate, which block of memory is unknown, in most cases the memory is not allocated or has no read and write permissions, using gets () The function writing data to it is obviously wrong. I strongly recommend assigning a value of null  to a pointer that is not initialized, for example: char *str = NULL; null is the meaning of " 0 value, equals zero " , which represents a null pointer in the C language. On the surface, a null pointer is a pointer that does not point to any data, is an invalid pointer, and the program does not use it to produce an effect. Note the case-sensitive,null does not have any special meaning, just an ordinary identifier. Many library functions judge the incoming pointer, and if it is a null pointer, do nothing, or give a hint. Change the code above, assign a value to str null , and see what effect it will have: 1. #include2. int main () {3. char * str =NULL; 4. Gets (str); 5. printf ("%s \ n", str); 6. return 0; 7. After running the program, it is found that the user has not entered any characters, and printf () directly outputs the (NULL)。 We have reason to infer that both get () and printf () have special handling of NULL pointers: • Gets () does not allow the user to enter a string, nor does it write data to the memory that the pointer points to; printf () does not read what the pointer points to, but simply gives a hint that the programmer realizes that a null pointer is being used. We can also make similar judgments in our own defined functions, for example: 1. void func (char * p) {2. if (p = = NULL) {3. printf ("(NULL) \ n"); 4.} else {5. printf ("%s \ n", p); 6.} 7. This can greatly increase the robustness of the program and prevent meaningless manipulation of null pointers. In fact, NULL isstdio.hA macro defined in: #define NULL ((void *) 0)(void *) 0Indicates that the value 0 is cast tovoid *Type, outermost( )To prevent ambiguity by enclosing the contents of the macro definition. On the whole, NULL points to memory with address 0 instead of pointing to any data as previously stated. In the virtual address space of a process, a memory area at the lowest address is called a retention zone, which does not store valid data or access by the user program, and it is easy to detect a violation pointer by pointing NULL to the area. With regard to the concept of virtual address space and the memory distribution of the program, we willC language and memoryTopic, readers only need to remember that in most operating systems, very small addresses typically do not save data and do not allow program access, and NULL can point to any address in this range of addresses. Note that the C language does not specify a null point, except that most standard library conventions idiomatic null to 0, so do not equate null with 0, for example, the following notation is unprofessional: int *p = 0; and should be persisted as: int *p = NULL; note The difference between meaning null and NUL: null represents a null pointer and is a macro definition that can be used directly from your code. Instead, NUL represents the end of the string, ' \ s ', which is the No. 0 character in the ASCII code table. NUL is not defined in the C language, it is merely a salutation to ' + ' and cannot be used directly in the code.void PointerThe contents of the macro definition for null pointers are only(void *) 0)Made a rough introduction, here's the point.void *The meaning. Void is used in a function definition to indicate that a function has no return value or no formal parameter, which is used here to indicate that the type of data pointed to by the pointer is unknown. Other wordsvoid *Represents a valid pointer, which does point to real data, except that the type of the data has not yet been determined and is typically forced to be cast during subsequent use. C language dynamic memory allocation function The return value of malloc () isvoid *Type, for coercion type conversions when used, see the following example:Plain Text Copy1. #include2. int main () {3.//Allocates memory that can hold 30 characters and converts the returned pointer to char. char * str = (char *) malloc (sizeof (char) *); 5. Gets ( STR); 6. printf ("%s \ n", str); 7. return 0; 8. Run Result: C.biancheng.netc.biancheng.net The point here is to make you understandvoid *, it is not the meaning of the null pointer, but the actual pointer, but the pointer to the memory does not know what type of data is stored. Source: Csdn
C language learning null pointers and void pointers