Thread: About NULL subject in C

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    About NULL subject in C

    Hi;
    NULL is considered as a pointer right? or its bi-sided, meaning it can be considered as value or as pointer?

    For example: I can write
    Code:
    int *p;
    p=NULL; // in this case its not pointing to any addresses!
    Also can write like
    Code:
    int *p;
    *p=NULL;
    so what's the differences between two cases? in second case also I'm having value NULL which is ofcourse by default its pointer NULL, isnt it? !

    thanks!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    If you enable the warnings of your compiler you'll that it warns you for converting a pointer to an integer. Also, that code is very likely to crash your program because p is an uninitialized pointer, and you must never dereference an uninitialized pointer!
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    so what's the differences between two cases?
    One case is attempting to set a value to NULL, and one case is attempting to set the pointer to NULL.

    Be careful with NULL, in many cases it can act like an int, but it is usually as defined as a ((void *)0), you should never try to use NULL on anything other than a pointer.

  4. #4
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by jimblumberg View Post
    One case is attempting to set a value to NULL, and one case is attempting to set the pointer to NULL.

    Be careful with NULL, in many cases it can act like an int, but it is usually as defined as a ((void *)0), you should never try to use NULL on anything other than a pointer.

    So, we can see, NULL is acting like a value called NULL ( 0 in int), and as a pointer pointing to nothing! right?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So, we can see,
    Yes, we can see that that original code is crap, you should never use NULL with anything but pointers. Using it for anything else is a very bad practice and may or may not work as you seem to expect it to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. On the subject of internet video
    By bernt in forum General Discussions
    Replies: 2
    Last Post: 05-20-2010, 06:20 PM
  2. Subject:Win'98 Desktop.
    By Adock in forum Tech Board
    Replies: 2
    Last Post: 09-17-2007, 01:11 PM
  3. Good Subject!
    By Melissa Atkin in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 12-27-2002, 04:09 PM
  4. Which subject????
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 04-20-2002, 11:06 AM
  5. Subject Descriptions
    By tim545666 in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2002, 04:59 AM

Tags for this Thread