473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2610
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
9
by: Christian Christmann | last post by:
Hi, I was just going through this exercise http://www.cas.mcmaster.ca/~franek/books/membook-answers/ch4/answers-ch4-3.html and I'am confused about the answer. It says: "... the compiler...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
7
by: jknaty | last post by:
I'm trying to create a function that splits up a column by spaces, and I thought creating a function that finds the spaces with CHARINDEX and then SUBSTRING on those values would an approach. I...
11
by: Antoninus Twink | last post by:
What's the correct syntax to define a function that returns a pointer to a function? Specifically, I'd like a function that takes an int, and returns a pointer to a function that takes an int and...
13
by: Sri Harsha Dandibhotla | last post by:
Hello all. I recently came across a function declaration as : char(*(*x()))(); This was not in some code but it was in a C questions thread on some group. I tried to decipher what it returns but...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.