Thread: what is a null pointer?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    what is a null pointer?

    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!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    for your knowledge,

    NULL is nothing inside on any variable,

    or the actual value is "-1"

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    Wink

    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).
    C++ Is cool

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by beely
    for your knowledge,

    NULL is nothing inside on any variable,

    or the actual value is "-1"
    Um.. no. NULL always evaluates to zero. Period. It is not -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.
    Hope is the first step on the road to disappointment.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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.
    a conforming implementation does not have to define NULL as (void *)0, although many do; it can be a typeless 0.

    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
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Why am I getting these errors??
    By maxthecat in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2006, 01:00 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM