Thread: NULL struct

  1. #1
    Registered User
    Join Date
    Feb 2006

    NULL struct

    Hi,i got this struct :

    typedef struct {
    	int age;
    	int id;
    and got this in the main:
    student *a;
    I was trying to see if that struct node is empty or not.I was expecting it to return NULL because struct has no data.where am i go wrong?

    How to find if a struct is empty / null ?


  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    All you're doing is making a new pointer to a student variable. When you declare a variable like that and you don't initialize it, then it could contain any value, and therefore point somewhat randomly to any variable. Some compilers might initialize such a variable to 0 (ie. NULL for pointers), some may leave in any garbage value, others may set a certain bit pattern so you can recognize it's a variable that you didn't give any value to. Regardless, the important thing to realize is that the value of a local variable you didn't initialize yourself is undefined, which means anything can happen.

    When you declare a pointer, set it to something, if even NULL before you do testing or operations on it.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Also, you can not test (in a simple way) if a struct is empty. If you expect to have structures that are (say) in an array that may or may not be filled in, so you need to "check if this entry is empty", you have a few different choices:
    1. have a field like this "int filled_in", and if that field is zero (or some other defined value), it's empty.

    2. Fill all datastructures (using memset or similar) with a defined value (zero is almost certainly the "best" value here, but you could use for example 0xFF, 0xCC, 0xDEADBEEF or some such), and then write a function similar to memcmp that compares the whole structure as if it was a array of bytes or longwords with the defined value.

    3. Keep an external map of which values are filled in (e.g. a single-bit-per-entry) of "filled in" entries, where a one in the map means this entry is filled in, and a zero means "empty".

    4. Use a special value for one of the regular fields (e.g. id or age in your struct) - e.g say that an "id" of "-1" is "Empty". Initialize all structs to have "id=-1" and then check for that value to see if it's empty. Equally, a negative age may be considered "invalid", so "age == -1" would also be possible in this case.

    5. Think of some completely different way to store the data, so that you don't need to know if it's empty or not (because you don't have any empty data structures stored). For example using a linked list, binary tree or dynamically growing table of some sort.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM