472,127 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

function returns char *

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

Nov 5 '07 #1
5 2490
Travis <tr***********@gmail.comwrote 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
Nov 5 '07 #2

"Travis" <tr***********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.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() == "")
"" 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

Nov 5 '07 #3
On 2007-11-05 15:10:55 -0500, Travis <tr***********@gmail.comsaid:
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

Nov 6 '07 #4
"Travis" <tr***********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.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
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() == "" )
Nov 6 '07 #5
On Nov 6, 6:05 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
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.
Nov 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Java and Swing | last post: by
11 posts views Thread by Marco Loskamp | last post: by
17 posts views Thread by Razzel | last post: by
9 posts views Thread by Christian Christmann | last post: by
11 posts views Thread by Antoninus Twink | last post: by
13 posts views Thread by Sri Harsha Dandibhotla | last post: by
20 posts views Thread by MikeC | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.