functions returning *char

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rick

    functions returning *char

    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

  • Joona I Palaste

    #2
    Re: functions returning *char

    Rick <rrquick@nosp am-com> scribbled the following:[color=blue]
    > Hi,[/color]
    [color=blue]
    > 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?)[/color]

    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 (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "It sure is cool having money and chicks."
    - Beavis and Butt-head

    Comment

    • Mark A. Odell

      #3
      Re: functions returning *char

      Rick <rrquick@nosp am-com> wrote in news:3f8ea1c6@c larion.carno.ne t.au:
      [color=blue]
      > 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?)[/color]

      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 ->
      --

      Comment

      • Tristan Miller

        #4
        Re: functions returning *char

        Greetings.

        In article <3f8ea1c6@clari on.carno.net.au >, Rick wrote:[color=blue]
        > 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?)[/color]

        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

        Comment

        • Dan Pop

          #5
          Re: functions returning *char

          In <3f8ea1c6@clari on.carno.net.au > Rick <rrquick@nosp am-com> writes:
          [color=blue]
          >I was wondering, can it be safely assumed that any function that returns
          >a *char will return a NULL terminated string?[/color]

          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.
          [color=blue]
          >Or does the function
          >explicitly mention this always (and the ones that don't do not return
          >null terminated strings?)[/color]

          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@ifh.de

          Comment

          • Dan Pop

            #6
            Re: functions returning *char

            In <Xns941665F71D8 79CopyrightMark Odell@130.133.1 .4> "Mark A. Odell" <nospam@embedde dfw.com> writes:
            [color=blue]
            >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?[/color]

            Well, malloc and friends return a void pointer...
            [color=blue]
            >You need to read the function description to know if it returns a null
            >terminated string.[/color]

            Correct.

            Dan
            --
            Dan Pop
            DESY Zeuthen, RZ group
            Email: Dan.Pop@ifh.de

            Comment

            • CBFalconer

              #7
              Re: functions returning *char

              Dan Pop wrote:[color=blue]
              > "Mark A. Odell" <nospam@embedde dfw.com> writes:
              >[color=green]
              > > 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?[/color]
              >
              > Well, malloc and friends return a void pointer...[/color]

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

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

              Comment

              • Ron Croonenberg

                #8
                Re: functions returning *char

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

                Ron


                Rick wrote:[color=blue]
                > 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
                >[/color]

                Comment

                • Irrwahn Grausewitz

                  #9
                  Re: functions returning *char

                  Ron Croonenberg <ronc@depauw.ed u> wrote:[color=blue]
                  >
                  >Rick wrote:[color=green]
                  >> 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?)
                  >>[/color]
                  >Nope, it returns the pointer to a string...that's it.
                  >[/color]

                  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
                  (irrwahn33@free net.de)

                  Comment

                  • Keith Thompson

                    #10
                    Re: functions returning *char

                    Rick <rrquick@nosp am-com> writes:[color=blue]
                    > 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?)[/color]

                    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_Keit h) kst@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"

                    Comment

                    • pete

                      #11
                      Re: functions returning *char

                      Dan Pop wrote:[color=blue]
                      >
                      > In <3f8ea1c6@clari on.carno.net.au > Rick <rrquick@nosp am-com> writes:
                      >[color=green]
                      > >I was wondering, can it be safely assumed that
                      > >any function that returns a *char will return a
                      > >NULL terminated string?[/color]
                      >
                      > 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.
                      >[color=green]
                      > >Or does the function
                      > >explicitly mention this always (and the ones that don't do not return
                      > >null terminated strings?)[/color]
                      >
                      > 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.[/color]

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

                      --
                      pete

                      Comment

                      • Peter Nilsson

                        #12
                        Re: functions returning *char

                        Irrwahn Grausewitz <irrwahn33@free net.de> wrote in message news:<0k0uovsv1 dnqmo0eulpmian6 pbbp5jek65@4ax. com>...[color=blue]
                        > Ron Croonenberg <ronc@depauw.ed u> wrote:[color=green]
                        > >
                        > >Rick wrote:[color=darkred]
                        > >> 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?)
                        > >>[/color]
                        > >Nope, it returns the pointer to a string...that's it.[/color]
                        >
                        > 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. ;-)[/color]

                        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

                        Comment

                        • Irrwahn Grausewitz

                          #13
                          Re: functions returning *char

                          airia@acay.com. au (Peter Nilsson) wrote:
                          [color=blue]
                          >Irrwahn Grausewitz <irrwahn33@free net.de> wrote in message news:<0k0uovsv1 dnqmo0eulpmian6 pbbp5jek65@4ax. com>...[color=green]
                          >> Ron Croonenberg <ronc@depauw.ed u> wrote:[color=darkred]
                          >> >
                          >> >> 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.[/color]
                          >>
                          >> 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. ;-)[/color]
                          >
                          >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.[/color]

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

                          Right, but to avoid possible confusion or misunderstandin gs 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
                          (irrwahn33@free net.de)

                          Comment

                          • Irrwahn Grausewitz

                            #14
                            Re: functions returning *char

                            pete <pfiland@mindsp ring.com> wrote:
                            [color=blue]
                            >Dan Pop wrote:[color=green]
                            >>
                            >> In <3f8ea1c6@clari on.carno.net.au > Rick <rrquick@nosp am-com> writes:
                            >>[color=darkred]
                            >> >I was wondering, can it be safely assumed that
                            >> >any function that returns a *char will return a
                            >> >NULL terminated string?[/color][/color][/color]
                            <snip>[color=blue][color=green]
                            >> 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.[/color]
                            >
                            >strstr, strtok, strchr, strrchr, and strpbrk
                            >can all return either a null pointer or a pointer to a string.[/color]

                            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
                            (irrwahn33@free net.de)

                            Comment

                            • Irrwahn Grausewitz

                              #15
                              Re: functions returning *char

                              Irrwahn Grausewitz <irrwahn33@free net.de> wrote:
                              [color=blue]
                              >airia@acay.com .au (Peter Nilsson) wrote:
                              >[/color]
                              <snip>[color=blue][color=green]
                              >>[BTW, strings are null-terminated by definition.][/color]
                              >
                              >Right, but to avoid possible confusion or misunderstandin gs 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.[/color]

                              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
                              (irrwahn33@free net.de)

                              Comment

                              Working...