Results 1 to 4 of 4

Thread: null pointer

  1. #1

    null pointer

    what is null pointer in c and c++ and why it is used?


  2. #2
    Junior Member
    Join Date
    Jul 2007
    Answers
    5

    Re: null pointer

    Quote Originally Posted by vasundhara.jagdale View Post
    what is null pointer in c and c++ and why it is used?
    A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address. This value is the result of type-casting the integer value zero to any pointer type.

    int * p;
    p = 0; // p has a null pointer value

    I Think..u can search in google...for better understanding..as to y this error occurs..wat exactly happens etc...


  3. #3
    Expert Member
    Join Date
    Mar 2012
    Answers
    208

    Re: null pointer

    The null pointer constant does not to point to any real object. We can assign it to any pointer variable since it has type void *.
    The NULL pointer points to an address of 0x00000000 (32 bits) of memory. The computer systems have reserved this zero address for NULL pointer and cannot be used for other purposes.


  4. #4
    Contributing Member
    Join Date
    Jun 2010
    Answers
    55

    Re: null pointer

    The null pointer is a well-defined "nowhere" that is guaranteed to compare unequal to any valid memory address. It can be used in several different ways:

    • Indicate an error condition - standard library functions such as fopen and malloc will return a null pointer if they cannot complete an operation such as opening a file or allocating memory;
    • Indicate the end of input or of a sequence - functions like fgets or strtok will return NULL if there's no more input (end of file or end of string);
    • Indicate a pointer is not currently being used - since NULL is well-defined, it's easy to check against, so any pointer variable that's not currently meant to point anywhere should be set to NULL (in general, it's impossible to know if a non-NULL pointer value is valid or not just from the pointer value itself);


    Note that there is a null pointer value (the address used by the underlying operating system to indicate "nowhere") and the null pointer constant (used to define the NULL macro). The operating system may use any address it wants to for NULL; it could be 0x00000000, it could be 0xFFFFFFFF, it could be 0xDEADBEEF, etc. However, as far as your source code is concerned, a zero-valued integral expression indicates a NULL pointer. The macro NULL is defined as either a plain 0, or as (void *) 0, or any other integral expression that evaluates to 0.

    When the compiler translates your source code to machine code, it will replace all those instances of 0 or NULL with the correct null pointer value.


  5.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact