The correct answer to this question is that your teacher is not wrong, and he/she's not ignorant either. He/she is just teaching you good habits. Using an explicit cast is not a must, but there are at least two very good reasons why you should.
Code Portability
Using an explicit cast ensures your code is portable between
ANSI C compilers, pre-ANSI compilers, and most importantly, non ANSI
C compliant compilers. Your teacher's code should not generate
errors or warnings with any compiler, compliant or non-compliant,
past, present or future.
If compiling on any platform that pre-dates ANSI C or is not ANSI
compliant, writing the code:
pMyObject = NULL;
could, on some systems, generate compiler warnings along the lines of:
warning: '=' conversion from 'void*' to 'struct foo*'
warning: '=' conversion from 'int' to 'struct foo*'
The purpose of the compiler warning is to alert you to a potential problem, therefore code that compiles with even a single warning should not be considered production-quality code unless the warning is fixed with an explicit cast.
Code Readability
Using an explicit cast greatly improves readability. For example, take the following real world example based on the Microsoft Windows SDK:
hMyWindow = CreateWindow(NULL, NULL, WM_OVERLAPPED, 0, 0, 100, 100, NULL, NULL, NULL, NULL);
if (hMyWindow == NULL)
...
Unless you refer to the Microsoft Windows SDK, it is obvious that you will not understand what parameters are being passed to this function. However, if I write:
hMyWindow = CreateWindow((LPCTSTR) NULL, (LPCTSTR) NULL, WM_OVERLAPPED, 0, 0, 100, 100, (HWND) NULL, (HMENU) NULL, (HINSTANCE) NULL, (LPVOID) NULL);
if (hMyWindow == (HWND) NULL)
...
then I know, in an instant: 1) the type of variable hWindow
, 2) return type for CreateWindow()
, 3) type of every function parameter. Virtually everything I need to know to understand this code, and I don't have to scan the current file, or look for another header file, or waste time browsing the internet looking for the API documentation. This makes code self
documenting, and this is especially important if you are in one of the many non-graphical environments
that do not have built-in code browsing support.
In ANSI C99 (ISO/IEC 9899:1999, Section 6.5.16.1), ANSI agreed on the concept of the NULL pointer constant, and that assignment of the NULL pointer constant to a pointer of a typed object was legal.
However the real reason your teacher taught you the above method is because he/she is simply being just like any good teacher and preparing you for the real world, and in the real world the ANSI C standard is not the law. It does not have to be followed, and in most cases it is not followed. Compiler companies ignore or extend any standard if there is financial profit or market share to be gained. Furthermore, there are lots of proprietary computer systems in existence, some by the world's biggest manufacturers, where non-ANSI C compliant compilers are installed and are being used to compile C programs because they have to support non-standard hardware.
This is why using an explicit cast when doing any pointer operation involving NULL
is a very good habit to get into. It will ensure that your code is quickly understandable, compiles cleanly, and works as expected across the massive variety of platforms and compiler tools you will encounter in later life.