The difference between C ++ '/0' null and 0

Source: Internet
Author: User

today, I encountered a problem that I have never noticed before, Program :
const char * CP = "some value ";
while (* CP) {
// do something to * CP
++ CP;
}< br> when * CP points to '/0', the program stops. Before that, I always thought '/0' is a space. The output result is a space when '/0' is tested by a useful program. So I started to check the information and found my original idea was wrong.

The ASCII code of '/0' is 0, which is also called in C ++NULL character, The output result is similar to a space with no information. It is not a space;

The test is as follows:
Char A = '/0 ';
Cout <(INT) A <Endl;
If (A = 0)
Cout <"A value is 0" <Endl;
If (A = '')
Cout <"A is a space" <Endl ;;
Cout <"*" <A <"*" <Endl;
I finally found the answer,While ('/0') is equivalent to while (0) (the expression used as a condition statement is automatically converted to the bool type )...,

Null is no problem. You can directly use vs (go to definition) to view the result, that is, 0 (void *) 0; so when int * p = 0, that is, when an empty object is initially started, 0 is forcibly converted to (int *) 0.

therefore, C ++ programmers prefer to use 0 to initialize a pointer variable, instead of using null as a member of the C program .

Related Article

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.