will someone could explain more about null pointer for me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

will someone could explain more about null pointer for me?

null pointer

24th Jul 2016, 6:56 AM
Yousef Sayadi
Yousef Sayadi - avatar
5 Answers
+ 3
A null pointer points to an invalid address. Usually this invalid address is 0 (the pointer points to byte 0 in memory). It's a way of saying "this pointer is invalid". Many, many other addresses are invalid (and therefore pointers pointing to them) but a null pointer, by convention, contains the one value everyone knows is invalid.
24th Jul 2016, 11:06 AM
Stefan
Stefan - avatar
+ 2
@Harkirat: That's not really true, technically. First, NULL is a macro and not a constant. Second, this macro is also not a pointer. The macros value "0" is an integer literal that will be filled in by the preprocessor when NULL is used in the source code. Conceptually, NULL should only be used in the context of null pointers but can actually be used in every place where "0" is a valid expression. C++11 introduced the keyword "nullptr" that resolves the issues that arise from using a macro that is defined as an integer literal.
24th Jul 2016, 12:50 PM
Stefan
Stefan - avatar
+ 2
@Harkirat: I know it's a little late, but here's my answer to your "purpose of using macros" question: Well, for the simple macros as presented in this thread, the preprocessor just replaces the macro name in the source code by the text given for the macro.This is a *very* general way of modifying your source code. It is also potentially a very error-prone one, as the programmer actually does not see what the real source code is since the preprocessor modifies it before compilation (example: Many solutions in the Obfuscated C Code Contest on http://www.ioccc.org/ use macros to hide the actual source code to a degree that is just mind-frying). Also, for many applications of macros there are more readable and type-safe solutions (e.g. constants, why not define a const variable? It makes type checks possible and easier to understand and does not replace text in unintended places). Still, for some cases code generation or modification is very help as it prevents a lot of boring and error-prone boilerplate coding. That's why libraries use it, but that's the only case in which macros should be used as there are better solutions for the problems that were once solved by macros (not just C++11 and later). Side note: Macros are still commonly used to control the definition of certain classes/structs/functions in libraries. I find library configuration to be the task of the build system, so I would not define these macros in the source code but define them in the build system, i.e. via compiler switches.
26th Jul 2016, 3:08 AM
Stefan
Stefan - avatar
+ 2
My pleasure :-)
26th Jul 2016, 10:57 AM
Stefan
Stefan - avatar
0
pointers have some reserved values is called null pointer. null pointer means it is pointing nowhere.
3rd Aug 2017, 4:39 PM
Nilesh Halge
Nilesh Halge - avatar