By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,591 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

function returns char *

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
"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

P: n/a
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.