I would like to know what is a null pointer?
Is the data type of the null pointer that is to be returned important?
thanks!
Printable View
I would like to know what is a null pointer?
Is the data type of the null pointer that is to be returned important?
thanks!
A null pointer is a pointer that points specificly to nothing. That is to say: You implicitly set it to point to nothing. Consider the following:
char *ptr;
'ptr' points to some random space in memory because you haven't assigned it to point to anything.
char *ptr = NULL;
'ptr' is now pointing specificly to nothing. This means you can check the pointer to see if it is actually pointing at nothing, and if it is, then you can allocate space for it, or do whatever you want.
Commonly NULL is used to check for the end of lists or error states or what not.
It's better to have a null pointer than an uninitialized one, IMO.
Quzah.
for your knowledge,
NULL is nothing inside on any variable,
or the actual value is "-1"
null pointers
is an integral constant expression that evaluates to zero, or such
an expression cast to type void *, is converted to a pointer called
the “null pointer.” This pointer is guaranteed to compare unequal
to a pointer to any valid object or function (except for pointers to
based objects, which can have the same offset and still point to
different objects).
Um.. no. NULL always evaluates to zero. Period. It is not -1.Quote:
Originally posted by beely
for your knowledge,
NULL is nothing inside on any variable,
or the actual value is "-1"
printf("%d", NULL );
You're probably confusing return values or something. Commonly -1 is returned for errors (obvious exceptions are things like stcpy() which can return -1 and this doesn't indicate error status).
Quzah.
Quote:
Originally posted by beely
for your knowledge,
NULL is nothing inside on any variable,
or the actual value is "-1"
Code:/* _null.h
Definition of NULL.
*/
/*
* C/C++ Run Time Library - Version 7.0
*
* Copyright (c) 1987, 1996 by Borland International
* All Rights Reserved.
*
*/
#ifndef NULL
# if !defined(__FLAT__)
# if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
# define NULL 0
# else
# define NULL 0L
# endif
# else
# if defined(__cplusplus) || defined(_Windows)
# define NULL 0
# else
# define NULL ((void *)0)
# endif
# endif
#endif
a conforming implementation does not have to define NULL as (void *)0, although many do; it can be a typeless 0.Quote:
Originally posted by Salem
A NULL pointer has a type (void*), you should never try and dereference a NULL pointer, and on some machines, if you every try it - your program will die.
also i would like to mention if your implementation kills a program when you derefrence a null pointer, you are lucky; some will just modify the memory contained there, often leading to worse problems :eek: