Instead Of NULL, Should I Write `0x0` Or `0`? - C++ - Stack Overflow
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs Instead of NULL, should I write `0x0` or `0`? Ask Question Asked 15 years ago Modified 5 years, 3 months ago Viewed 39k times 18I know that you are supposed to use 0 instead of NULL in c++ (even though NULL is defined as 0 in C++ most of the time).
Recently I came across some code where 0x0 was used instead, though.
What is the difference?
Share Improve this question Follow edited Dec 13, 2011 at 17:46 Lightness Races in Orbit 384k77 gold badges663 silver badges1.1k bronze badges asked Nov 19, 2009 at 17:42 mokamoka 3651 gold badge5 silver badges10 bronze badges 5- Who says you're supposed to use 0 instead of NULL? Personally, I think you should use "(void*)0". – Paul Tomblin Commented Nov 19, 2009 at 17:47
- 9 @Paul: (void*)0 will not work similarly in C++. C++ compilers get angry when they see something like: int* x = (void*)0; while int* x = 0; is OK. – Mehrdad Afshari Commented Nov 19, 2009 at 17:53
- Yeah, I was more opining about the design of the language than suggesting you actually do it that way. – Paul Tomblin Commented Nov 19, 2009 at 17:56
- 1 @Paul: Not allowing implicit conversion of void* to other pointer types is one place where C++ broke compatibility with C (and for a reason). – UncleBens Commented Nov 19, 2009 at 21:31
- @Paul: "I think you should use" "I was more [..] than suggesting you actually do it that way" – Lightness Races in Orbit Commented Dec 13, 2011 at 17:47
8 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 290x0 is just 0 written in hexadecimal notation. There is no difference between the two:
016 = 010 :)
NULL is usually #defined to 0 somewhere and does the same thing.
Share Improve this answer Follow answered Nov 19, 2009 at 17:43 Mehrdad AfshariMehrdad Afshari 421k92 gold badges859 silver badges794 bronze badges 2- And = 0_8, which is what the question asks about. – Lightness Races in Orbit Commented Dec 13, 2011 at 17:48
- Also, as 0b0(binary) and 00(octal). Although, most compilers' preprocessor changes these to 0. – Artfaith Commented Aug 7, 2019 at 18:56
There is no difference. In C, NULL is often defined to be (void *)0, but in C++ that's not allowed. A few ancient compilers got this wrong, but they really are ancient.
IMO, it's better to use NULL, as it portrays the intent more clearly, and gives you a nice, easy symbol to S&R when your compiler gets updated to C++ 0x, which will include nullptr.
Share Improve this answer Follow answered Nov 19, 2009 at 17:49 Jerry CoffinJerry Coffin 489k83 gold badges647 silver badges1.1k bronze badges 1- +1. Up to very recently I was party to using 0 instead of NULL, but the inclusion of nullptr in C++0x made me change my mind. – Gorpik Commented Nov 26, 2009 at 16:55
By the way, everyone,
0x0 is hex, as you have all mentioned, but
0 is Octal, not decimal! :-)
ie any number starting with 0 (and not followed by x) is octal:
0 = 0 01 = 1 02 = 2 ... 07 = 7 010 = 8 011 = 9 012 = 10 ...:-)
Share Improve this answer Follow answered Nov 19, 2009 at 18:37 tonytony 3,8571 gold badge20 silver badges18 bronze badges 3- 2 I was going to down vote you for sowing confusion. But I had to check the standard first ... and you're right. Learn something new everyday (not that this is worth remembering, but I will anyways). – deft_code Commented Nov 19, 2009 at 22:26
- 1 that's exactly how it lives in my brain - I know it is totally useless, but yet I remember it. – tony Commented Nov 20, 2009 at 2:27
- 1 Correct, but not an answer to the question. – Lightness Races in Orbit Commented Dec 13, 2011 at 17:48
0x0 and 0 represent the same value, so you can use the one that best suits your eye. When we get C++0x, we'll have the keyword nullptr to represent null pointers of any type.
Share Improve this answer Follow answered Nov 19, 2009 at 17:48 Michael KristofikMichael Kristofik 35.1k16 gold badges77 silver badges127 bronze badges Add a comment | 20x0 is just an expression of the value 0 in hexadecimal. I doubt the compiler will care about the difference.
Share Improve this answer Follow answered Nov 19, 2009 at 17:44 postfuturistpostfuturist 22.6k11 gold badges66 silver badges85 bronze badges Add a comment | 2No difference
In my opinion, 0x0 is more explicit.
Some people may confuse 0 with "0"(0x30 -I´ve seen it happen).
Share Improve this answer Follow answered Nov 19, 2009 at 17:44 TomTom 45k30 gold badges141 silver badges171 bronze badges 4- 4 0x0000 is even more explicit! – Roger Pate Commented Nov 19, 2009 at 17:59
- 1 @Roger: nah, that's only more explicit on a 16bit machine. On a 32bit machine it's still not explicit enough ;-) – Steve Jessop Commented Nov 19, 2009 at 18:09
- 2 +1 for giving me a laugh. But if you are running a 64 bit compiler you should use 0x0000000000000000 for maximum explicitness. – Gunther Piez Commented Nov 19, 2009 at 18:11
- Actually, on a 64 bit machine it may make sense to define NULL as 0LL, so sizeof(NULL)==sizeof((void*)NULL). – MSalters Commented Nov 20, 2009 at 9:06
Simply put, its a matter of taste. People like to write memory addresses in hexa decimal, so writing NULL as 0x0 would be more classic. For ordinary ones like me, 0 will suffice.
Share Improve this answer Follow answered Nov 19, 2009 at 18:25 Shailesh KumarShailesh Kumar 6,8878 gold badges37 silver badges60 bronze badges Add a comment | -8So, this site has some really good information on NULL: http://c-faq.com/null/index.html
NULL is actually the "null pointer" - so it might be of type, for example, "(void*)0". This tells the compiler that NULL is not to be treated as an int, but rather as a "pointer to an int". And in C, the compiler will give warnings/errors if the types don't match.
The value "0" and "0x0" are equivalent in C++. Whenever a number is prefixed by "0x", it means that that value is the hexadecimal representation of that number. For example, 0x5 == 5.
Also, 0xA [in hexadecimal, base 16) == 10 [in base 10].
edit
Here are some sources. In glibc, NULL seems to be defined differently in different places. (why should this be?)
#define NULL ((void *)0) (stdlib/gmp-impl.h)
#define NULL 0 (posix/getopt1.c)
edit2
well, it looks like i've missed the mark on this one! sorry! (CW'd. feel free to keep downvoting!)
Share Improve this answer Follow edited Nov 26, 2009 at 16:48 community wiki 3 revspoundifdef 3- 6 In C++, NULL will not be (for example) "(void*)0" -- that's clearly not allowed. Though it's probably a typo, that would be treated as point to void, not pointer to int. – Jerry Coffin Commented Nov 19, 2009 at 18:11
- This answer is wrong. NULL is not the "null pointer". It merely converts to a "null pointer" of a particular type. Also you are linking to a C site on a C++ question. (even in C, NULL is not necessarily a "null pointer"). – Johannes Schaub - litb Commented Nov 26, 2009 at 11:15
- Notice that your linked site says "NULL is defined as a null pointer constant" - not as a null pointer! – Johannes Schaub - litb Commented Nov 26, 2009 at 11:16
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Draft saved Draft discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- Your docs are your infrastructure
- Featured on Meta
- More network sites to see advertising test [updated with phase 2]
- We’re (finally!) going to the cloud!
- Call for testers for an early access release of a Stack Overflow extension...
Related
207 Do you use NULL or 0 (zero) for pointers in C++? 3 What is the difference between NULL and "0"? 2 What is NULL, does it need to be declared? 17 Whats better to use in C++11 , Zero or NULL? 1 Is using NULL and '\0' in condition statements the same? 14 Null-terminate string: Use '\0' or just 0? 59 Can '\0' and NULL be used interchangeably? 2 Are 'null' and 'NULL' the same? 0 What are the differences bettween null character and '\0'? 4 What is the special status of the value 0 in c++?Hot Network Questions
- How can I solve my equation with the best numerical precision?
- What's the Purpose of the IRQ on a 6502
- "Elegant" conditions on two quadratics (with positive real roots) to ensure that the larger root of one is less than the smaller root of the other
- Identifying a TNG episode where Dr. Pulaski instructs another doctor on a sling
- Groups with no proper non-trivial fully invariant subgroup
- What is this Stardew Valley item?
- Is Holy Terra Earth?
- Perfect eden - Whence conflict?
- Static vs dynamic certificate pinning
- The British used to (still?) classify their guns by weight in pounds rather than caliber. Was that a constant across shell types?
- What's a good way to append a nonce to ciphertext in Python for AES GCM in Python?
- cyan flash experience
- What exactly is the cornerstone that Mark 12:10 speaks of?
- Solving Large size problems in industry: Tips and Tricks
- Publishing corollaries of previously published results
- Non-Schengen flight without any passport control, any repercussion on a non-EU traveller
- Sorites paradox and emergence
- Table structure with multiple foreign keys and values
- Do I need Letter of invitation to Iceland?
- How important is storytelling when submitting a grant application (or even a science paper)?
- Can you make 5 x 3 “magic” rectangles?
- If someone’s words are likely to be disregarded, how to describe the quality of “meaning” they lack?
- Is BNF grammar in TeXbook correct?
- How can I avoid overusing her/she or the character name when describing character action
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-cppTừ khóa » C = 0x0 && C 0x81
-
Bitwise Operators In C
-
Binary Logic Bit Operations In C And C++ - Somacon
-
Endpoint Explanation
-
Solved: OV2740 UVC Request To Set Exposure Time
-
Solved: CyCx3UvcAppUSBEventCB Never Executed
-
Panic: Runtime Error: Cgo Argument Has Go Pointer To Go Pointer #6
-
Interpreting SCSI Sense Codes In VMware ESXi And ESX (289902)
-
Extract Integers From Binary Based… | Apple Developer Forums
-
Media Write Error (84) | Spiceworks Storage
-
ESXi 6.5 And 6.7 Host Fails With PSOD When IPV6 Is Globally ...