? about pointer variables?

This is a discussion on ? about pointer variables? within the C Programming forums, part of the General Programming Boards category; Hello, can someone tell me why or what of dereferenceing a pointer variable, which has the value NULL? I saw ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question ? about pointer variables?

    Hello,
    can someone tell me why or what of dereferenceing a pointer variable, which has the value NULL? I saw this in the book I was reading and could not find what it meant by that.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Dereferencing a NULL pointer results in a segmentation violation. It's not something you want to happen. Run this and see how a segv occurs on your system:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int *p = NULL;
      *p = 10;
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    A NULL pointer is a pointer that points to... "nothing". (NULL is defined as 0, or (void*)0 ).

    Dereferencing a pointer means using the asterisk to get the value it points to.

    Code:
    // ...
    int *pointer;
    int a = 7;
    pointer = &a;
    
    cout << *pointer << endl; // dereferences pointer and outputs 7
    Sorry for using cout... as that's C++.. but you get the picture

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by rmullen3
    A NULL pointer is a pointer that points to... "nothing". (NULL is defined as 0, or (void*)0 ).

    Dereferencing a pointer means using the asterisk to get the value it points to.

    Code:
    // ...
    int *pointer;
    int a = 7;
    pointer = &a;
    
    cout << *pointer << endl; // dereferences pointer and outputs 7
    Sorry for using cout... as that's C++.. but you get the picture
    null pointing to zero is a compiler specific thing
    hello, internet!

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Thanks, Prelude, the program "vomitted".

    (Like you knew it wouldn't!)

    The 'type' is undefined, but may be re-defined later. Sans that, get the "barf bags" out!

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Pointer variables
    By xixpsychoxix in forum C Programming
    Replies: 1
    Last Post: 10-29-2008, 08:05 AM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 04:20 AM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21