i want to ask about NULL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nazedyra
    New Member
    • Jul 2014
    • 1

    i want to ask about NULL

    what is the function of NULL. can some one explain it to me about this:=
    srand (time(NULL));
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    NULL is a macro (defined in <stddef.h> and elsewhere). It is the implementation-defined null pointer constant. A pointer with this value is understood to not point at anything. Conversely, a pointer with any other value can be assumed to point to something.

    null has no special technical meaning. It has the standard dictionary meaning.

    NUL is the name of the ASCII null character. The character code for NUL is 0, which coincidentally is the value of the null character in C ('\0'). The null character is used to terminate strings.

    Refer to the manpage for time to see what that function does when its argument is NULL.
    Refer to the manpage for srand to see what that function does with the value passed to it.
    Last edited by donbock; Jul 17 '14, 09:00 PM. Reason: Added null character.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Since NULL has no special meaning it is often used as a zero.

      Anytime you use NULL and expect it to be zero, you are coding a potential bug since your code may not work on some compilers.

      Defensive coding practice would ask that you use 0 for zero rather than use NULL.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Hmmm. Maybe I'm wrong about not using NULL for function pointers. Refer to C FAQ 5.8.

        NULL is the null pointer value for data pointers. The C Standard permits compiler implementations to use different encoding rules for data pointers and function pointers if that makes sense for the particular environment. Portable code does not assign data pointer values to function pointers or vice-versa. Thus, portable code will not assign NULL to function pointers. The portable null pointer value for function pointers is the bare integer 0. I have been known to use the following private macro for the null function pointer constant:
        Code:
        #define FNULL 0
        By the way, the "%p" format code for printf is for data pointers. I can't think of a maximally portable way to print out the value of a function pointer.
        Last edited by donbock; Jul 17 '14, 10:48 PM. Reason: Added preface that questions the accuracy of this post.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          There you are. FNULL for function pointers and NULL for data pointers. I can say that in the 25 years I have been around C/C++ code, I have never seen FNULL.

          I am not believer in macros because the preprocessor may not expand them in the way I expect and the compiler has no way to verify the expansion is correct.

          Personally, I hard-code a 0 in both cases. That, to me, is a null value.

          Comment

          Working...