functions returning *char

Discussion in 'C Programming' started by Rick, Oct 16, 2003.

  1. Rick

    Rick Guest

    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
    Rick, Oct 16, 2003
    #1
    1. Advertising

  2. 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "It sure is cool having money and chicks."
    - Beavis and Butt-head
    Joona I Palaste, Oct 16, 2003
    #2
    1. Advertising

  3. Rick <rrquick@nospam-com> wrote in news::

    > 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 ->
    --
    Mark A. Odell, Oct 16, 2003
    #3
  4. Greetings.

    In article <>, 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
    Tristan Miller, Oct 16, 2003
    #4
  5. Rick

    Dan Pop Guest

    In <> 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:
    Dan Pop, Oct 16, 2003
    #5
  6. Rick

    Dan Pop Guest

    In <Xns941665F71D879CopyrightMarkOdell@130.133.1.4> "Mark A. Odell" <> 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:
    Dan Pop, Oct 16, 2003
    #6
  7. Rick

    CBFalconer Guest

    Dan Pop wrote:
    > "Mark A. Odell" <> 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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 16, 2003
    #7
  8. 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
    >
    Ron Croonenberg, Oct 16, 2003
    #8
  9. Ron Croonenberg <> 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
    ()
    Irrwahn Grausewitz, Oct 16, 2003
    #9
  10. 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) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Oct 17, 2003
    #10
  11. Rick

    pete Guest

    Dan Pop wrote:
    >
    > In <> 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
    pete, Oct 17, 2003
    #11
  12. Irrwahn Grausewitz <> wrote in message news:<>...
    > Ron Croonenberg <> 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
    Peter Nilsson, Oct 17, 2003
    #12
  13. (Peter Nilsson) wrote:

    >Irrwahn Grausewitz <> wrote in message news:<>...
    >> Ron Croonenberg <> 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
    ()
    Irrwahn Grausewitz, Oct 17, 2003
    #13
  14. pete <> wrote:

    >Dan Pop wrote:
    >>
    >> In <> 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
    ()
    Irrwahn Grausewitz, Oct 17, 2003
    #14
  15. Irrwahn Grausewitz <> wrote:

    > (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
    ()
    Irrwahn Grausewitz, Oct 17, 2003
    #15
  16. Rick

    Dan Pop Guest

    In <> pete <> writes:

    >Dan Pop wrote:
    >>
    >> In <> 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:
    Dan Pop, Oct 17, 2003
    #16
  17. Rick

    Dan Pop Guest

    In <> Irrwahn Grausewitz <> writes:

    >Irrwahn Grausewitz <> wrote:
    >
    >> (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:
    Dan Pop, Oct 17, 2003
    #17
  18. (Dan Pop) wrote:

    >In <> Irrwahn Grausewitz <> 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
    ()
    Irrwahn Grausewitz, Oct 17, 2003
    #18
  19. Irrwahn Grausewitz <> 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.
    Christopher Benson-Manica, Oct 17, 2003
    #19
  20. Christopher Benson-Manica <> wrote:

    >Irrwahn Grausewitz <> 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
    ()
    Irrwahn Grausewitz, Oct 17, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    542
  2. wwj
    Replies:
    24
    Views:
    2,484
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    462
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    571
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
Loading...

Share This Page