someone pliz help me to explain what is NULL
i'm so confused about ut
thx b4
someone pliz help me to explain what is NULL
i'm so confused about ut
thx b4
NULL is a macro defined in <stddef.h> to be a null pointer constant. This means that it is typically defined as 0 or ((void*)0).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It's also defined in <stdio.h>
EDIT: FAQ
Last edited by BEN10; 11-26-2009 at 08:31 AM.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
hmm
thx u all
but I still don't understand what is utility of NULL
sry
but I'm still beginner in programming ^^
No, NULL is implementation defined, and a standard conforming C++ implementation will not define NULL as ((void*)0) in <cstddef>.Originally Posted by RockyMarrone
What do you understand of pointers and null pointers?Originally Posted by firedream
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
Right -- because the value of a pointer is a memory address. Guess where 0 is?
You could also assign "2" to a pointer and it will respond like it is NULL, because 2 is not a valid address either.
One useful thing about NULL is that it is 0. 0 is the only value in C that will fail a truth test:
"blah blah" will not happen if ptr == 0 (NULL). Notice that many C functions will return a NULL pointer to indicate an error or some kind of failure. Eg, if you use strstr(), which searches for a substring (a pattern, such as "wo") in a longer string (such as "hello world") you get a pointer to the beginning of "wo" if it is found, and a NULL pointer if it is not. If it is found you probably want to do something that you will not want to do otherwise, so the truth test is very useful -- really, it is the basic and fundamental concept in programming.Code:char *ptr = NULL; if (ptr) { blah blah
Nb., a pointer assigned and invalid address (like 2) is effectively NULL and will behave that way but it will pass a truth test. So will an uninitialized pointer. Which is why we use NULL
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
@MK27
Will ptr be allocated no memory in this case too? It's surprising!!Code:int *ptr; ptr=10; //or any number
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
No, semantically a null pointer does not point to any memory location, not even address 0.Originally Posted by MK27
Not quite:Originally Posted by MK27
Originally Posted by C99 Section 6.3.2.3 Paragraphs 3 to 5
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Technically, looking at the quote from laserlight (the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation) I suppose it is more or less "undefined" what will happen, but realistically 2 is not a valid address so there can be no memory there.
Another interesting point about that correction is that two NULL pointers will not == one another, which I had not noticed this, so there is bit more to NULL than it just being
#define NULL 0
which I thought it was exactly that.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Eh, I think you misread. The standard states that "any two null pointers shall compare equal", not that they shall not compare equal.Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge