function returns char *

T

Travis

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
 
J

Joe Greer

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
 
M

Mike Wahler

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?

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
 
K

Kira Yamato

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.
 
J

Jim Langston

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?

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() == "" )
 
T

terminator

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 )
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top