A Preliminary Study on null values in C Language

Source: Internet
Author: User

In C, what is the null value? What does it mean?

For more information, see du Niang. Here we will only describe my understanding of the null value.

 

Simply put, the null value represents "inaccessible ".

 

See the following example:

# Include <stdlib. h> <br/> # include <stdio. h> <br/> # include <string. h> </P> <p> int main (INT argc, char * argv []) <br/> {<br/> char * s; <br/> S = (char *) malloc (10 * sizeof (char); <br/> If (S = NULL) {<br/> printf ("malloc error! /N "); <br/> return-1; <br/>}< br/> memset (S, 0, 10 * sizeof (char )); <br/> If (S = NULL) {<br/> printf ("Something error! /N "); <br/> return-1; <br/>}< br/> memset (S, '0', 10 * sizeof (char )); <br/> If (S = NULL) {<br/> printf ("Something error2! /N "); <br/> return-1; <br/>}< br/> printf (" All OK! /N "); <br/> return 0; <br/>}

 

What is the final result of this example? Yes all OK!

 

If you change char * s to Char s [10]; or char s [];

The result will be clearer. The same is all OK!

 

Question:

The definitions of char * s and char s [] are different in C language implementation. The latter determines the variable during compilation and assigns the address to it;

Char * s is dynamically generated during running. Therefore, the former requires memory allocation and check if (S = NULL), if s! = NULL,

The variable is allocated successfully!

 

Based on this, whether the null value is 0, '0', or (void *) 0 is actually not important. They are different definitions of different compilers, just superficial phenomena.

In essence, null indicates that the address is inaccessible!

 

For personal opinions, please contact us!

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.