NULL or '

Thread: NULL or '\0'

'

This is a discussion on NULL or '

Thread: NULL or '\0'

'
within the C++ Programming forums, part of the General Programming Boards category; hi , There are 2 questions here. I would appreciate your help here. 1.i want to know what you all ...

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    NULL or '\0'

    hi ,
    There are 2 questions here. I would appreciate your help here.

    1.i want to know what you all prefer to use ->NULL or '\0' ??like for example to count the length of string. and also why do you guys choose so. and also is it that using NULL over '\0' is advantagious while coding or vice vesra??? I just want to know these so that i code better in future. ;-)

    2. I think using inline doesnt make any difference .like example
    Code:
    inline int a(int b) { //stuff }
    
              and
    
    int a(int b) {//stuff}
    so whats the point in using inline when the results are same.Is it that using inline saves compile time or something else?
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    1. NULL resides in <windows.h> and is not part of the C++ standard. It's traditionally used in Windows programs. The number '0' can be used just as well.
    '\0' is another thing; it's the null-character, which is not the same thing.

    2. There's no functional difference between inlined and non-inlined functions. It can make your program a little bit faster (and larger) when applied to small functions.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77
    Originally posted by Sang-drax
    '\0' is another thing; it's the null-character, which is not the same thing.

    well you didnt catch my point here. okay....
    Code:
    while (* a_string++ != NULL ) 
    {
     // do something.
    }
    
    //we can use a  '/0' instead of NULL can't we?? but why
    // is it that programmer chose one over the other??
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>NULL resides in <windows.h> and is not part of the C++ standard.
    Actually, NULL is defined in <cstdio> among others and is quite standard. :-)

    >>i want to know what you all prefer to use ->NULL or '\0'
    NULL is used for pointers, '\0' is used for character strings, the two are completely different. It's not as big an issue in C++ because NULL is most likely defined as
    Code:
    #define NULL 0
    but in C and less than state of the art C++ compilers you should never use NULL in place of '\0' because it could be defined like this
    Code:
    #define (void *)0
    which would cause your program to choke and die :-)

    >>I think using inline doesnt make any difference
    inline only makes a difference if you use the function a *lot*, otherwise it could have no effect on performance and could actually slow things down.

    >>//we can use a '/0' instead of NULL can't we??
    Maybe, but it's not a good idea

    >>// is it that programmer chose one over the other??
    He was misinformed :-)
    *Cela*

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The value NULL is used for pointers, it indicates that the pointer points to nothing.

    The value '\0' is used for strings, it indicates the end of a so-called null-terminated string. Such a string has '\0' as last character.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,702
    You can also use 0 in place of '\0' as in:

    while(*p++ != 0);

    Likewise, for pointers, you can use NULL or 0 as in:

    char * p = 0;
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    also, for the majority of compilers, declaring a funciton as "inline" is just a suggestion to the compiler. if it doesnt think the function should be inline, it will not be made inline despite how it is declared... on the other hand if it is a small in class definition it might be made inline even if its not declared so.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There is typically a command that will force the compiler to inline code (compilers usually take the size of the function in account in order to decide whether or not to inline it). If your compiler doesn't support such a feature you probably do support a feature that throws warning when a function is not inlined. If it isn't inlined then can use a macro instead of an inline function. This does not apply to member functions of classes/structs though. When it comes to those your code is at the whim of the compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21