Thread: Null pointer

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    4

    Null pointer

    hi.. i have a doubt on what is a null pointer (is it a value or an address) and what does assigning null to a variable means ??..and how does it varies from a void pointer

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It might help to read this FAQ on null pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    The FAQ should answer your questions.

    Null pointer is of pointer type, which is usually an address.

    A null pointer can't point to any object or function -- a void pointer is a normal pointer that can point to any valid object/function, or null, but with void type - so no information on what the pointer points to. This means you can't dereference a void pointer -- you have to cast it to something first.
    A common example of use of a void pointer is malloc. malloc returns a void*, because it can be cast to whatever you're allocating memory for.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A NULL pointer is a pointer who's value compares equal to zero, according to rules of the language. By convention, a pointer with value NULL (or zero) means it does not point at a valid object. Dereferencing a NULL pointer (i.e. accessing whatever the pointer points at) therefore yields undefined behaviour.

    A void pointer is a completely different beast from a NULL pointer: a void pointer refers to a variable of type void *. Given only a void pointer, the compiler has no information about what type of object (int, float, a massive struct, etc) is pointed at, so attempting to dereference a void pointer yields a compilation error. However, if the programmer KNOWS the the type of object pointed at, the void pointer can be converted to a pointer to that type, and that new pointer can be dereferenced (although if the programmer gets the type conversion wrong, the behaviour is undefined). A void pointer (like a pointer to any type) can have a NULL value (indicating it does not point at any valid object).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    4
    Thank u........very heplful and that was very clear indeed....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Null Pointer
    By saswatdash83 in forum C Programming
    Replies: 3
    Last Post: 07-15-2008, 04:12 AM
  2. pointer ot NULL
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2003, 03:43 PM
  3. what is a null pointer?
    By forest in forum C Programming
    Replies: 6
    Last Post: 11-02-2002, 11:02 AM
  4. pointer always NULL?
    By endo in forum C++ Programming
    Replies: 5
    Last Post: 08-14-2002, 03:10 AM
  5. Null pointer
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:49 PM

Tags for this Thread