C language null and 0 differences and null explanations

Source: Internet
Author: User
Tags define null
First look at the output of the following piece of code:

#include <stdo.h>
int main ()
{
    int * p = NULL;
    printf ("% s", p);
 
}
Output <null>, single step debugging can see the execution of int * p = NULL, the value of p is 0x00000000, it can be seen that NULL is 0 in the actual underlying call,

In C language,

The value of NULL and 0 are the same, but for the purpose and purpose and easy to identify, NULL is used for pointers and objects, and 0 is used for numeric values


For the end of the string, use ‘\ 0’, its value is also 0, but let people know at a glance that this is the end of the string, not a pointer, nor an ordinary numeric value

In different systems,

NULL is not always the same as 0. NULL only represents a null value, that is, it points to an unused address. In most systems, 0 is used as an unused address, so there is a definition like this

#define NULL 0

But this is not always the case, and some systems do not use address 0 as NULL, but use other addresses. So, do not make NULL equal to 0, especially in some cross-platform code, this is even more Will bring you disaster.

See the explanation below:

Q 0 ‘0‘ ‘\ 0’ "\ 0"

 

To me, when doing C / C ++:

 

0 would digit zero, that is, a numerical value.

// 0 may be the number 0, which is a numeric value.

 

‘0‘ could be the character capital oh or the character zero. For example: char word [10] = "Oxford"; char number [10] = "01234";

 

Depending on typeface used ‘O‘ may look exactly like ‘0’ making it difficult to tell them apart out of context.

 // The above two sentences must be understood together. 0 may be used as the capital letter O or the character 0. Using "O" just based on the font looks like ‘0’ and it is difficult to distinguish them.

‘\ 0’ is the null character used to terminate strings in C / C ++.

// ‘\ 0’ is the null character used to abort the C / C ++ string

 

"\ 0" is an empty string.

// "\ 0" is an empty string

NULL is defined in stdio.h:

#if! defined (NULL) && defined (__ NEEDS_NULL)
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *) 0)
#endif
#endif
Defined as 0 in c ++ and (void *) 0 in c; why, reference: http://stackoverflow.com/questions/7016861/null-pointer-in-c-and-c

In the process of inquiry, I found a post below. Very good, COPY is as follows.
Reprinted from: http://blog.chinaunix.NET/u/18517/showart_309917.html


    This article is transferred from: http://bbs.chinaunix.net/viewthread.php?tid=544415&extra=&page=7 The post discusses the concepts of Null Pointer, Null Pointer Constant, NULL, 0 in C language and their relationship and difference. Here is a summary of whyglinux brother. Be a label, hehe ^ _ ^
What is a null pointer constant?

[6.3.2.3-3] An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant // The value of an integer constant expression is 0, or such expression type void *, Called the null pointer constant
Tell us here: 0, 0L, '\ 0', 3-3, 0 * 17 (they are all "integer constant expression") and (void *) 0 (tyc: I think (void *) 0 should be regarded as an empty Pointers, more appropriately) etc. are all null pointer constants (note that (char *) 0 is not called a null pointer constant, just a null pointer value). As for which form the system chooses to use as a null pointer constant, it is implementation-dependent. The general C system chooses (void *) 0 or 0 (there are also individual choices 0L); as for the C ++ system, due to strict type conversion requirements, void * cannot be freely converted to other pointer types as in C, so Normally choose 0 as the null pointer constant (tyc: recommended by the C ++ standard) instead of (void *) 0.

What is a null pointer?

[6.3.2.3-3] 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.//If a null pointer constant is Converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to be unequal to a pointer to any object or function.
char * p = 0; at this time p is a null pointer and does not point to any actual object.
Therefore, if p is a pointer variable, after any of p = 0 ;, p = 0L ;, p = '\ 0' ;, p = 3-3 ;, p = 0 * 17; For C, it can also be p = (void *) 0;), p all become a null pointer, the system guarantees that the null pointer does not point to any actual object or function. Conversely, the address of any object or function cannot be a null pointer. (Tyc: For example, (void *) 0 here is a null pointer. Understanding it as a null pointer or a null pointer constant will have a microsecond difference, of course, it does not matter)

What is NULL?

[6.3.2.3-Footnote] The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant // The macro defines empty <stddef.h> (and other headers) as a null pointer constant

That is, NULL is a standard-defined macro definition used to represent a null pointer constant. Therefore, in addition to the various assignment methods above, you can also use p = NULL; to make p a null pointer. (Tyc: implementation in many systems: #define NULL (void *) 0, which is not completely consistent with the "a null pointer constant" here)

Where does the null pointer point to memory (the internal implementation of the null pointer)?

The standard does not stipulate where the null pointer points to the memory, that is, which specific address value (0x0 address or a specific address) represents the null pointer depends on the implementation of the system. Our common null pointers generally point to the 0 address, that is, the interior of the null pointer is represented by all zeros (zero null pointer); there are also some systems that use some special address values or special methods to represent the null pointer (nonzero null pointer) , Non-zero null pointer), see C FAQ for details.

Fortunately, in actual programming, we do not need to know whether a null pointer is a zero null pointer or a nonzero null pointer on our system. We only need to know whether a pointer is a null pointer-the compiler will automatically implement one of them Conversion, shielding us from the implementation details. Note: Do not equate the internal representation of the null pointer to the object representation of the integer 0-as mentioned above, sometimes they are different.

How to judge whether a pointer is a null pointer?

This can be achieved by comparison with a null pointer constant or other null pointers (note that it has nothing to do with the internal representation of null pointers). For example, suppose p is a pointer variable and q is a null pointer of the same type. To check whether p is a null pointer, you can use any of the following forms-they are all equivalent in function and different It's just the difference in style.

The judgment that the pointer variable p is a null pointer:
if (p == 0)
if (p == ‘\ 0‘)
if (p == 3-3)
if (p == NULL) / * The use of NULL must include the corresponding standard library header files * /
if (NULL == p)
if (! p)
if (p == q)
...

The judgment that the pointer variable p is not a null pointer:
if (p! = 0)
if (p! = ‘\ 0‘)
if (p! = 3-3)
if (p! = NULL) / * The use of NULL must include the corresponding standard library header files * /
if (NULL! = p)
if (p)
if (p! = q)
...

Can I use the memset function to get a null pointer?

This question is equivalent to: if p is a pointer variable, then

memset (& p, 0, sizeof (p)); and p = 0;

Is it equivalent?

The answer is no, although it is equivalent on most systems, but because some systems have "nonzero null pointers" (nonzero null pointers), the two are not equivalent at this time. For this reason, it should be noted that when you want to set the pointer to a null pointer, you should not use memset, but should use a null pointer constant or a null pointer to assign or initialize a pointer variable.

Can I define my own implementation of NULL? Also answer "Can the value of NULL be 1, 2, 3 etc.?" Similar question

[7.1.3-2] If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined./ / If the program declares or defines an identifier reserved in a context (other than that allowed in 7.1.4), or defines the reserved identifier as a macro name, its behavior is undefined.

NULL is a reserved identifier in the standard library that meets the above conditions. Therefore, if the corresponding standard header file is included and NULL is introduced, it is illegal to redefine NULL to a different content in the program, and its behavior is undefined. In other words, if it is a standard-compliant program, its NULL value can only be 0, and it cannot be other than 0, such as 1, 2, 3, etc.

Does the malloc function return 0 or NULL when allocating memory fails?

The malloc function is a standard C library function. The standard specifies that a "null pointer" is returned when its memory allocation fails:

[7.20.3-1] If the space cannot be allocated, a null pointer is returned.//If the space cannot be allocated, a null pointer will be returned.

For null pointer values, general documents (such as man) tend to use NULL instead of directly saying 0. But we should be clear: for pointer types, returning NULL and returning 0 are completely equivalent, because both NULL and 0 mean "null pointer" (null pointer). (Tyc: NULL is returned in the manual in the general system, then we use NULL)
In addition, the explanation of the null pointer on the C FAQ is attached: C FAQ: null pointer

Baidu Encyclopedia explained;

\ 0 is the end marker of the string in C ++, stored at the end of the string. For example, char cha [5] indicates that a string Section, and c / c ++ like an array cha [5], there are 5 variables, namely cha [0], cha [1], cha [2], cha [3], cha [4] [5] You can put 5 letters or 2 Chinese characters (1 Chinese character takes 2 bytes, 1 letter takes 1 byte), cha [5] takes 5 bytes of memory space.

  E.g:

 
     char str [5];
     str [0] = ‘a’;
     str [1] = ‘b’;
     str [2] = ‘c’;
     str [3] = ‘d’;
     // str [4] = ‘\ 0‘; // abcd
     str [4] = ‘e’; / * This output is abcde and a bunch of garbled characters, and even jumps out of system errors, because there is no string end character
     printf ("% s", str);
  
C language NULL and 0 difference and NULL detailed explanation
of 5 characters can be placed. Since c / c ++ stipulates that the end of the string is '\ 0', although it does not count into the string length, it takes up memory space, and a Chinese Generally use two words 

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.