function returns char *

Discussion in 'C++' started by Travis, Nov 5, 2007.

  1. Travis

    Travis Guest

    I am using a function that returns a const char * that is usually a
    word, etc. How can I check to see if what it returns is empty?

    I tried if (function() == "") and (function() == NULL) and (function()
    == '/0').

    But then I see that the those if statements are flagging true when the
    function returns back a char * or no length
    Travis, Nov 5, 2007
    #1
    1. Advertising

  2. Travis

    Joe Greer Guest

    Travis <> wrote in news:1194293455.901800.171470
    @e9g2000prf.googlegroups.com:

    > I am using a function that returns a const char * that is usually a
    > word, etc. How can I check to see if what it returns is empty?
    >
    > I tried if (function() == "") and (function() == NULL) and (function()
    > == '/0').
    >
    > But then I see that the those if statements are flagging true when the
    > function returns back a char * or no length
    >
    >


    I'm not quite sure of your specification for the function function(),
    but
    assuming....

    // Obviously the real function would have to have the possibility of a
    // a 0 or "" result
    const char * function()
    {
    return "Hello";
    }

    ..
    ..
    ..

    const char * p = function();
    if (p == 0 || *p == '\0')
    {
    // String is empty
    }


    The condition can be swapped around if you want the opposite test:

    if (p != 0 && *p != '\0')
    {
    // string is not empty
    }

    HTH,

    joe
    Joe Greer, Nov 5, 2007
    #2
    1. Advertising

  3. Travis

    Mike Wahler Guest

    "Travis" <> wrote in message
    news:...
    >I am using a function that returns a const char * that is usually a
    > word, etc. How can I check to see if what it returns is empty?
    >
    > I tried if (function() == "")


    "" evaluates to a string literal which is converted
    to a pointer to that literal (iow it's a (non-NULL)
    address). This pointer value will always be different
    from the string you return (unless you're returning
    the literal "", which means it *might* have the same
    value).

    > and (function() == NULL) and (function()


    The boolean value of NULL is false, but if you're returning
    the address of a string (emtpy or not), it will have a non-NULL
    address, thus a boolean value of true.

    > == '/0').


    It's not valid to compare a pointer value with a character
    value. Did your compiler give a warning? Anyway, here
    you're again comparing the address of the string with
    something, not determining if it's empty or not.

    >
    > But then I see that the those if statements are flagging true when the
    > function returns back a char * or no length


    A C-style string ends with a zero-value (null) character.
    An empty string has no characters before this null. To
    determine emptiness, check the first character.

    if (*function())
    ; /* not empty, contains at least one character */
    else
    ; /* string is empty (zero length) */

    But if there's any chance that function() could return
    NULL, then the expression *function() will give
    undefined behavior. So check first:

    char *p = function();
    if(p && !*p) /* "if p is not NULL and string is empty..."
    {
    ; /* etc */
    }

    -Mike
    Mike Wahler, Nov 5, 2007
    #3
  4. Travis

    Kira Yamato Guest

    On 2007-11-05 15:10:55 -0500, Travis <> said:

    > I am using a function that returns a const char * that is usually a
    > word, etc. How can I check to see if what it returns is empty?


    What I'm about to say maybe off topic, but since you've posted to a C++
    newsgroup I suppose I should discuss the prefered way to do this in C++.

    Use std::string instead of const char *.

    Then your code can read something like this: if (function() == "") ...

    >
    > I tried if (function() == "") and (function() == NULL) and (function()
    > == '/0').


    None of the above will work in general. Read Mike Wahler's post for
    explanations.

    >
    > But then I see that the those if statements are flagging true when the
    > function returns back a char * or no length


    --

    -kira
    Kira Yamato, Nov 6, 2007
    #4
  5. Travis

    Jim Langston Guest

    "Travis" <> wrote in message
    news:...
    >I am using a function that returns a const char * that is usually a
    > word, etc. How can I check to see if what it returns is empty?
    >
    > I tried if (function() == "") and (function() == NULL) and (function()
    > == '/0').
    >
    > But then I see that the those if statements are flagging true when the
    > function returns back a char * or no length


    For a char* pointing to a c-style string with no length, the first byte of
    what it points to would be 0. so
    if ( function()[0] == '\0' )
    or
    if ( function()[0] =0 0 )
    or
    if ( strcmp( function(), "" == 0 )
    should work.

    However, since this is C++ std::string is probably a better bet. Then you
    can do
    if ( function() == "" )
    Jim Langston, Nov 6, 2007
    #5
  6. Travis

    terminator Guest

    On Nov 6, 6:05 am, "Jim Langston" <> wrote:

    > For a char* pointing to a c-style string with no length, the first byte > of


    so huried and full of writing mistakes.

    > what it points to would be 0. so
    > if ( function()[0] == '\0' )
    > or
    > if ( function()[0] =0 0 )


    this was intended:
    if ( function()[0] ==0 )

    > or
    > if ( strcmp( function(), "" == 0 )


    you meant this:
    if ( strcmp( function(), "" )== 0 )

    > should work.
    terminator, Nov 6, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vizlab
    Replies:
    3
    Views:
    4,161
    Michael Bar-Sinai
    Oct 17, 2007
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    987
    Ian Collins
    May 9, 2006
  3. ++imanshu
    Replies:
    7
    Views:
    447
    ++imanshu
    Aug 23, 2008
  4. Trans
    Replies:
    2
    Views:
    199
    Trans
    Nov 6, 2005
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    580
    David A. Black
    Jul 2, 2008
Loading...

Share This Page