Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The correct answer to this question is that your teacher is <strong>not wrong</strong>, and he/she's <strong>not ignorant</strong> 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.</p> <ol> <li><p><em>Code Portability</em></p> <p>Using an explicit cast ensures your code is <strong>portable</strong> 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.</p> <p>If compiling on any platform that pre-dates ANSI C or is not ANSI compliant, writing the code:</p> <pre> pMyObject = NULL; </pre> <p>could, on some systems, generate compiler warnings along the lines of:</p> <pre> warning: '=' conversion from 'void*' to 'struct foo*' warning: '=' conversion from 'int' to 'struct foo*' </pre> <p>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.</p></li> <li><p><em>Code Readability</em></p> <p>Using an explicit cast greatly improves readability. For example, take the following real world example based on the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms632679%28v=vs.85%29.aspx" rel="nofollow">Microsoft Windows SDK</a>:</p> <pre> hMyWindow = CreateWindow(NULL, NULL, WM_OVERLAPPED, 0, 0, 100, 100, NULL, NULL, NULL, NULL); if (hMyWindow == NULL) ... </pre> <p>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:</p> <pre> hMyWindow = CreateWindow((LPCTSTR) NULL, (LPCTSTR) NULL, WM_OVERLAPPED, 0, 0, 100, 100, (HWND) NULL, (HMENU) NULL, (HINSTANCE) NULL, (LPVOID) NULL); if (hMyWindow == (HWND) NULL) ... </pre> <p>then I know, <em>in an instant</em>: 1) the type of variable <code>hWindow</code>, 2) return type for <code>CreateWindow()</code>, 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 <strong>self documenting</strong>, 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.</p></li> </ol> <p>In ANSI C99 (ISO/IEC 9899:1999, Section 6.5.16.1), ANSI agreed on the concept of the <em>NULL pointer constant</em>, and that assignment of the NULL pointer constant to a pointer of a typed object was legal.</p> <p>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 <em>most</em> cases it is not followed. Compiler companies ignore or extend <em>any</em> standard if there is financial profit or market share to be gained. Furthermore, there are <strong>lots</strong> 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.</p> <p>This is why using an explicit cast when doing any pointer operation involving <code>NULL</code> 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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload