C/Explaining NULL
Expert: Zlatko - 8/19/2011
QuestionHow to explain NULL to students since they donot get the concept of strings,and ask about their real life examples of null
AnswerHello Aditya
The idea of NULL should be no more difficult than the idea of zero or nothing. Don't make it seem too mysterious when explaining it to students. NULL itself is defined as zero by the compiler.
NULL is used in two different contexts. In pointers, a NULL pointer is one which points to address 0. It is no different than any other pointer referring to any other address. By convention, address 0 is considered an invalid address so when we want to say that a pointer is not pointing to anything valid, then we set it to NULL or 0. In some data structures, linked lists for example, the records in the data structure point to other records. The final record in a linked list points to nothing, so we set that record's pointer to NULL to indicate that it is the last one in the list. It is helpful to draw a linked list diagram to show the concept of pointers. See for example
http://en.wikipedia.org/wiki/Linked_list
Strings are simply characters in consecutive bytes in memory. By convention, the end of the string is denoted by a zero byte, which we also call NULL. Explaining the concept of C strings is easily done by drawing a diagram of consecutive boxes, each box representing a byte with a character in it and the last byte containing a zero. The string resides somewhere in memory, and we use a pointer to the first byte to indicate the location of the string. See for example the first diagram in en.wikipedia.org/wiki/String_(computer_science)
I hope that helps you out.
Best regards
Zlatko