What is "null" in C? What is it? What is the purpose? How to use it?

Source: Internet
Author: User
Tags define null

This problem has actually plagued me for a long time. I have asked many people what is "null? What's the purpose?

Most people answer: "null is a special 0 defined by the system. Pointing your initialized pointer to it can prevent the evil effects of" wild pointer. "

Today, I met a C-language pen question, which is still a good question ~~~

Exp 1:

 
# Include <stdio. h> void fun (int * node) {static int n = 100; node = & N;} int main () {int * node = NULL; int A = 0; fun (node); A = * node; printf ("% d \ n", a); Return 0 ;}

Please answer. What are the output results of the above question?

From the results, this question can differentiate the degree of mastery of the C language. The result is: 100? 0? Error section exit? Which one is the cause? Why?

The first result person is actually deceived by the keyword "static", but "static" is a modifier of "N, the change to N will not be released after '}' of the fun function ~~~ Another point is that C-language functions always pass values (except for arrays, haha ~~~), So if you want to change the pointer direction (address value), you must pass the pointer unless you use return ~~~

The person who thinks it is the second result has mastered the points of the first result and knows that null is defined in the C language as follows:

 
# UNDEF null # If defined (_ cplusplus) # define null 0 # else # define null (void *) 0) # endif

However, GCC found that it was a segment error and exited ~~~

The question is in this sentence:

 
A = * node;
 
/* Perform the * operation on the node. If the node is changed, it is still (void *) 0 pointer ~~~ So a segment error occurs. Of course, you can read the value of null itself, that is, 0, but it is invalid to read the value it points, it will cause a segment error (it seems that this pointer error is also: the operating system limits the address space for user access, and the address space allocated by the memory (usually exists in hundreds of KB embedded systems ), in addition, there are three types. Of course, the wild pointer may also be directed to the legitimate address of the general user, and then it will be changed, and then it will be out of control ~~~) */

Null is a good thing. It gives a birth pointer to an anchor ~~~

PS: I thought it was 0 at the beginning, but I was not very competent ~~~ Practice and Practice ~~~

Exp 2:

 
# Include <stdio. h> int main (...) {int * iptr1 = NULL; int * iptr2 = 0; // error: Invalid conversion from 'int' to 'int * 'int * iptr3 = 1; // error: invalid conversion from 'void * 'to 'int *' int * iptr4 = (void *) 0); Return 0 ;}

Well, the first two initialization definitions are correct. The first error indicates that 0 is a special number and should be taken care of by the compiler, but isn't the second macro that matches null? But there is a problem with the pointer type, so what is going on? Does the compiler take special care of the null keyword?

Form understanding and using C pointers

PS: The book also seems to have explained this problem.

To be continued ~~~

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.