Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

functions returning *char

 
 
pete
Guest
Posts: n/a
 
      10-17-2003
Dan Pop wrote:
>
> 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.


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

--
pete
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      10-17-2003
Irrwahn Grausewitz <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> 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.


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
 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-17-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Peter Nilsson) wrote:

>Irrwahn Grausewitz <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
>> Ron Croonenberg <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-17-2003
pete <(E-Mail Removed)> wrote:

>Dan Pop wrote:
>>
>> 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?

<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
((E-Mail Removed))
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-17-2003
Irrwahn Grausewitz <(E-Mail Removed)> wrote:

>(E-Mail Removed) (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
((E-Mail Removed))
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-17-2003
In <(E-Mail Removed)> pete <(E-Mail Removed)> writes:

>Dan Pop wrote:
>>
>> 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.

>
>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: (E-Mail Removed)
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-17-2003
In <(E-Mail Removed)> Irrwahn Grausewitz <(E-Mail Removed)> writes:

>Irrwahn Grausewitz <(E-Mail Removed)> wrote:
>
>>(E-Mail Removed) (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: (E-Mail Removed)
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-17-2003
(E-Mail Removed) (Dan Pop) wrote:

>In <(E-Mail Removed)> Irrwahn Grausewitz <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-17-2003
Irrwahn Grausewitz <(E-Mail Removed)> 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.
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-17-2003
Christopher Benson-Manica <(E-Mail Removed)> wrote:

>Irrwahn Grausewitz <(E-Mail Removed)> 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
((E-Mail Removed))
 
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