Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > functions returning *char

Reply
Thread Tools

functions returning *char

 
 
Rick
Guest
Posts: n/a
 
      10-16-2003
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

 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      10-16-2003
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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It sure is cool having money and chicks."
- Beavis and Butt-head
 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      10-16-2003
Rick <rrquick@nospam-com> wrote in news:(E-Mail Removed):

> 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 ->
--
 
Reply With Quote
 
Tristan Miller
Guest
Posts: n/a
 
      10-16-2003
Greetings.

In article <(E-Mail Removed)>, 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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-16-2003
In <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-16-2003
In <Xns941665F71D879CopyrightMarkOdell@130.133.1.4> "Mark A. Odell" <(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-16-2003
Dan Pop wrote:
> "Mark A. Odell" <(E-Mail Removed)> 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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Ron Croonenberg
Guest
Posts: n/a
 
      10-16-2003
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
>


 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-16-2003
Ron Croonenberg <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-16-2003
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) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Functions returning pointers JS C++ 9 03-16-2005 07:49 PM
Returning const reference value from functions Sree C++ 3 06-25-2004 11:50 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Functions returning arbitrary type Andrej Prsa C Programming 5 07-15-2003 01:34 AM



Advertisments