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

functions returning *char

P: n/a
Hi,

I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)

Rick

Nov 13 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Rick <rrquick@nospam-com> scribbled the following:
Hi, I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)


No, from merely seeing that a function returns a char*, you can't say
anything whether it will return a null-terminated string. It might, or
it might not.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It sure is cool having money and chicks."
- Beavis and Butt-head
Nov 13 '05 #2

P: n/a
Rick <rrquick@nospam-com> wrote in news:3f******@clarion.carno.net.au:
I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)


No, I would not assume char *func() returns a C string (null terminated).
What if you have a block of memory you want to return a pointer to? You
need to read the function description to know if it returns a null
terminated string.

--
- Mark ->
--
Nov 13 '05 #3

P: n/a
Greetings.

In article <3f******@clarion.carno.net.au>, Rick wrote:
I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)


Nope. A function returning a *char might not even return something pointing
to a string at all (i.e., when it returns the pointer value NULL). Always
read the function's documentation (or the code itself, if there is no
documentation) to determine what conclusions can be drawn regarding the
return value.

Note that you appear to be confusing some terminology with respect to
strings and pointers -- NULL (in capital letters) refers to a specific
pointer value denoting that the pointer does not point to any memory in
particular. On the other hand, the "null" in the phrase "null-terminated
string" refers to the character '\0' (named NUL in ASCII, though of course
you might not be using an ASCII system), which is used in C as an
end-of-string marker. The character '\0' and the pointer value NULL might
technically have the same integral value from a human's point of view, but
as they are of different types they cannot be considered equal or used
(either in code or in natural language) interchangeably.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #4

P: n/a
In <3f******@clarion.carno.net.au> Rick <rrquick@nospam-com> writes:
I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string?
There is no such thing as a NULL terminated string. There is no
connection whatsoever between the null character and the NULL macro.
Furthermore, all C strings are null terminated, by definition. But a
char pointer need not point to a string.
Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)


The function specification must explicitly mention it. Even strncpy may
not return a pointer to a string (if there was not enough space for the
null character). AFAICT, this is the only example from the standard C
library.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5

P: n/a
In <Xn********************************@130.133.1.4> "Mark A. Odell" <no****@embeddedfw.com> writes:
No, I would not assume char *func() returns a C string (null terminated).
What if you have a block of memory you want to return a pointer to?
Well, malloc and friends return a void pointer...
You need to read the function description to know if it returns a null
terminated string.


Correct.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

P: n/a
Dan Pop wrote:
"Mark A. Odell" <no****@embeddedfw.com> writes:
No, I would not assume char *func() returns a C string (null
terminated). What if you have a block of memory you want to
return a pointer to?


Well, malloc and friends return a void pointer...


I hope not. I would much prefer a pointer to void :-)

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #7

P: n/a
Nope, it returns the pointer to a string...that's it.

Ron
Rick wrote:
Hi,

I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)

Rick


Nov 13 '05 #8

P: n/a
Ron Croonenberg <ro**@depauw.edu> wrote:

Rick wrote:
Hi,

I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)

Nope, it returns the pointer to a string...that's it.


No, it returns a pointer to a character, which may or may not happen to
be the first character in a null-terminated string. /That's/ it. ;-)

BTW, please don't top-post, thank you.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #9

P: n/a
Rick <rrquick@nospam-com> writes:
I was wondering, can it be safely assumed that any function that
returns a *char will return a NULL terminated string? Or does the
function explicitly mention this always (and the ones that don't do
not return null terminated strings?)


A function that returns a *char is probably the result of a typo, and
is unlikely to return anything.

A function that returns a char*, on the other hand, may or may not
return a pointer to a nul-terminated string. If you're lucky, the
function's documentation will tell you (and if you haven't read the
documentation, why are you calling it in the first place?).

If your question is limited to functions in the C standard library,
the standard or any decent C reference book should tell you what the
function is expected to return.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #10

P: n/a
Dan Pop wrote:

In <3f******@clarion.carno.net.au> Rick <rrquick@nospam-com> writes:
I was wondering, can it be safely assumed that
any function that returns a *char will return a
NULL terminated string?


There is no such thing as a NULL terminated string. There is no
connection whatsoever between the null character and the NULL macro.
Furthermore, all C strings are null terminated, by definition. But a
char pointer need not point to a string.
Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)


The function specification must explicitly mention it.
Even strncpy may not return a pointer to a string
(if there was not enough space for the null character).
AFAICT, this is the only example from the standard C
library.


strstr, strtok, strchr, strrchr, and strpbrk
can all return either a null pointer or a pointer to a string.

--
pete
Nov 13 '05 #11

P: n/a
Irrwahn Grausewitz <ir*******@freenet.de> wrote in message news:<0k********************************@4ax.com>. ..
Ron Croonenberg <ro**@depauw.edu> wrote:

Rick wrote:
Hi,

I was wondering, can it be safely assumed that any function that returns
a *char will return a NULL terminated string? Or does the function
explicitly mention this always (and the ones that don't do not return
null terminated strings?)

Nope, it returns the pointer to a string...that's it.


No, it returns a pointer to a character, which may or may not happen to
be the first character in a null-terminated string. /That's/ it. ;-)


Under C99, a non-null char * points an array of indeterminate number
of chars as a pointer to a single object can be treated as an array of
1 such object.

[BTW, strings are null-terminated by definition.]

--
Peter
Nov 13 '05 #12

P: n/a
ai***@acay.com.au (Peter Nilsson) wrote:
Irrwahn Grausewitz <ir*******@freenet.de> wrote in message news:<0k********************************@4ax.com>. ..
Ron Croonenberg <ro**@depauw.edu> wrote:
>
>> I was wondering, can it be safely assumed that any function that returns
>> a *char will return a NULL terminated string? Or does the function
>> explicitly mention this always (and the ones that don't do not return
>> null terminated strings?)
>>
>Nope, it returns the pointer to a string...that's it.
No, it returns a pointer to a character, which may or may not happen to
be the first character in a null-terminated string. /That's/ it. ;-)


Under C99, a non-null char * points an array of indeterminate number
of chars as a pointer to a single object can be treated as an array of
1 such object.


Which doesn't make it a string, that's the point.
[BTW, strings are null-terminated by definition.]


Right, but to avoid possible confusion or misunderstandings I prefer the
term "null-terminated string" when talking about C strings.

Well, of course I could say: "array of indeterminate number of
characters holding a character sequence terminated by a null character",
but that's really bulky.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #13

P: n/a
pete <pf*****@mindspring.com> wrote:
Dan Pop wrote:

In <3f******@clarion.carno.net.au> Rick <rrquick@nospam-com> writes:
>I was wondering, can it be safely assumed that
>any function that returns a *char will return a
>NULL terminated string?
<snip> The function specification must explicitly mention it.
Even strncpy may not return a pointer to a string
(if there was not enough space for the null character).
AFAICT, this is the only example from the standard C
library.


strstr, strtok, strchr, strrchr, and strpbrk
can all return either a null pointer or a pointer to a string.


Right, but AFAICT Dan's concern was about functions that return a
valid pointer to a character that is not part of a null-terminated
string.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #14

P: n/a
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
ai***@acay.com.au (Peter Nilsson) wrote:

<snip>
[BTW, strings are null-terminated by definition.]


Right, but to avoid possible confusion or misunderstandings I prefer the
term "null-terminated string" when talking about C strings.

Well, of course I could say: "array of indeterminate number of
characters holding a character sequence terminated by a null character",
but that's really bulky.


More precisely I could call it: "A contiguous sequence of characters
terminated by and including the first null character."

or even: "Thing defined in ISO/IEC 9899:1999 7.1.1#1 first sentence".

;-)

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #15

P: n/a
In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
Dan Pop wrote:

In <3f******@clarion.carno.net.au> Rick <rrquick@nospam-com> writes:
>I was wondering, can it be safely assumed that
>any function that returns a *char will return a
>NULL terminated string?


There is no such thing as a NULL terminated string. There is no
connection whatsoever between the null character and the NULL macro.
Furthermore, all C strings are null terminated, by definition. But a
char pointer need not point to a string.
>Or does the function
>explicitly mention this always (and the ones that don't do not return
>null terminated strings?)


The function specification must explicitly mention it.
Even strncpy may not return a pointer to a string
(if there was not enough space for the null character).
AFAICT, this is the only example from the standard C
library.


strstr, strtok, strchr, strrchr, and strpbrk
can all return either a null pointer or a pointer to a string.


But whenever they return a valid pointer value, it's a pointer to a
string. That was my point.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #16

P: n/a
In <3s********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
ai***@acay.com.au (Peter Nilsson) wrote:

<snip>
[BTW, strings are null-terminated by definition.]


Right, but to avoid possible confusion or misunderstandings I prefer the
term "null-terminated string" when talking about C strings.

Well, of course I could say: "array of indeterminate number of
characters holding a character sequence terminated by a null character",
but that's really bulky.


More precisely I could call it: "A contiguous sequence of characters
terminated by and including the first null character."

or even: "Thing defined in ISO/IEC 9899:1999 7.1.1#1 first sentence".

;-)


Which is... "string" ;-) So, there is no risk of confusion or
misundestandings: anyone who doesn't get it is too confused for this
newsgroup, anyway ;-)

In a looser context, you can say "C string" to avoid any confusion.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #17

P: n/a
Da*****@cern.ch (Dan Pop) wrote:
In <3s********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
<snip>
More precisely I could call it: "A contiguous sequence of characters
terminated by and including the first null character."

or even: "Thing defined in ISO/IEC 9899:1999 7.1.1#1 first sentence".

;-)


Which is... "string" ;-) So, there is no risk of confusion or
misundestandings: anyone who doesn't get it is too confused for this
newsgroup, anyway ;-)


You're probably right on this. :)
In a looser context, you can say "C string" to avoid any confusion.


Which seems to have the best accuracy/brevity ratio of all.

BTW, I have to tune my guitar's D string; it sounds horrible. ;-)
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #18

P: n/a
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
BTW, I have to tune my guitar's D string; it sounds horrible. ;-)


Why not just replace it with a B string? Well, wait, I have no idea whether
strings even existed in B ;) I'm sure a C++ string would be too expensive,
and a C# string would probably sound just as bad, so maybe you should just
stick with your D string since it has character(s).

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #19

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Irrwahn Grausewitz <ir*******@freenet.de> spoke thus:
BTW, I have to tune my guitar's D string; it sounds horrible. ;-)


Why not just replace it with a B string? Well, wait, I have no idea whether
strings even existed in B ;) I'm sure a C++ string would be too expensive,
and a C# string would probably sound just as bad, so maybe you should just
stick with your D string since it has character(s).


And it doesn't change the overall tuning (C# would be a disaster).

--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.