Type of a string literal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s0suk3@gmail.com

    Type of a string literal

    Hi,

    I've heard that a string literal has type 'char *' (i.e., a pointer to
    a char object). But I'm confused as to why this works:

    char *GetString(void )
    {
    return "hello";
    }

    int main(void)
    {
    printf("%s\n", GetString());

    return 0;
    }

    And it does print 'hello'. But if the pointer that GetString() returns
    points to a local object, wouldn't the buffer be deallocated when the
    function returns, thus making the pointer point to nothing? Or does
    that mean that a string literal has type 'static char *'? Or more
    precisely, 'static const char *'?

    Thanks,
    Sebastian

  • santosh

    #2
    Re: Type of a string literal

    s0suk3@gmail.co m wrote:
    Hi,
    >
    I've heard that a string literal has type 'char *' (i.e., a pointer to
    a char object). But I'm confused as to why this works:
    A string literal has type array of char, but it automatically is
    converted to a pointer to it's first element under most contexts, as is
    common for all array types in C.
    char *GetString(void )
    {
    return "hello";
    }
    >
    int main(void)
    {
    printf("%s\n", GetString());
    >
    return 0;
    }
    >
    And it does print 'hello'. But if the pointer that GetString() returns
    points to a local object, wouldn't the buffer be deallocated when the
    function returns, thus making the pointer point to nothing? Or does
    that mean that a string literal has type 'static char *'? Or more
    precisely, 'static const char *'?
    >
    Thanks,
    Sebastian
    Source code string literals are placed in static arrays. Their type is
    of type char or wchar_t and are not qualified with const, but modifying
    them invokes undefined behaviour anyway.

    Comment

    • vippstar@gmail.com

      #3
      Re: Type of a string literal

      On Jul 26, 9:22 am, s0s...@gmail.co m wrote:
      Hi,
      >
      I've heard that a string literal has type 'char *' (i.e., a pointer to
      a char object). But I'm confused as to why this works:
      >
      char *GetString(void )
      {
      return "hello";
      >
      }
      See 6.4.5 p 5 from n1256.pdf. string literals are immutable and
      "replaced" in TP 7 by static storage duration arrays. Their actual
      type is char [N] where N is the integer returned by sizeof, or wchar_t
      [N] (for wide string literals).
      When evaluated, these become char * and wchar_t *.

      Notice that GetString lies to the programmer; the returned value
      cannot be modified! Unless you document that, your function is
      misleading.

      Comment

      • s0suk3@gmail.com

        #4
        Re: Type of a string literal

        On Jul 26, 5:03 am, vipps...@gmail. com wrote:
        On Jul 26, 9:22 am, s0s...@gmail.co m wrote:
        >
        Hi,
        >
        I've heard that a string literal has type 'char *' (i.e., a pointer to
        a char object). But I'm confused as to why this works:
        >
        char *GetString(void )
        {
            return "hello";
        >
        }
        >
        See 6.4.5 p 5 from n1256.pdf. string literals are immutable and
        "replaced" in TP 7 by static storage duration arrays. Their actual
        type is char [N] where N is the integer returned by sizeof, or wchar_t
        [N] (for wide string literals).
        When evaluated, these become char * and wchar_t *.
        >
        Notice that GetString lies to the programmer; the returned value
        cannot be modified! Unless you document that, your function is
        misleading.
        Thanks. As for the last part: would it be good practice to qualify the
        function definition with 'const'?

        Comment

        • Ben Bacarisse

          #5
          Re: Type of a string literal

          s0suk3@gmail.co m writes:
          On Jul 26, 5:03 am, vipps...@gmail. com wrote:
          >On Jul 26, 9:22 am, s0s...@gmail.co m wrote:
          >>
          Hi,
          >>
          I've heard that a string literal has type 'char *' (i.e., a pointer to
          a char object). But I'm confused as to why this works:
          >>
          char *GetString(void )
          {
              return "hello";
          >>
          }
          >>
          >See 6.4.5 p 5 from n1256.pdf. string literals are immutable and
          >"replaced" in TP 7 by static storage duration arrays. Their actual
          >type is char [N] where N is the integer returned by sizeof, or wchar_t
          >[N] (for wide string literals).
          >When evaluated, these become char * and wchar_t *.
          >>
          >Notice that GetString lies to the programmer; the returned value
          >cannot be modified! Unless you document that, your function is
          >misleading.
          >
          Thanks. As for the last part: would it be good practice to qualify the
          function definition with 'const'?
          Yes, const char *GetString(void ); would be better. Technically this
          is not qualifying the function type, nor is it even qualifying the
          function's return type. It is, as required, saying the function
          returns pointer to const qualified char.

          --
          Ben.

          Comment

          • Greg Comeau

            #6
            Re: Type of a string literal

            In article <1c88205d-10ef-4370-bd7e-c30752c4d226@l6 4g2000hse.googl egroups.com>,
            <s0suk3@gmail.c omwrote:
            >I've heard that a string literal has type 'char *' (i.e., a pointer to
            >a char object). But I'm confused as to why this works:
            >
            >char *GetString(void )
            >{
            return "hello";
            >}
            >
            >int main(void)
            >{
            printf("%s\n", GetString());
            >
            return 0;
            >}
            >
            >And it does print 'hello'. But if the pointer that GetString() returns
            >points to a local object, wouldn't the buffer be deallocated when the
            >function returns, thus making the pointer point to nothing? Or does
            >that mean that a string literal has type 'static char *'? Or more
            >precisely, 'static const char *'?
            Check out http://www.comeaucomputing.com/techtalk/#stringliteral
            plus some stuff just before and just after it. Enjoy! :)
            --
            Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
            Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
            World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
            Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

            Comment

            • Greg Comeau

              #7
              Re: Type of a string literal

              In article <174af926-0721-4b75-b2ab-353c6af58c98@m4 4g2000hsc.googl egroups.com>,
              <vippstar@gmail .comwrote:
              >On Jul 26, 9:22 am, s0s...@gmail.co m wrote:
              >Hi,
              >>
              >I've heard that a string literal has type 'char *' (i.e., a pointer to
              >a char object). But I'm confused as to why this works:
              >>
              >char *GetString(void )
              >{
              > return "hello";
              >>
              >}
              >
              >See 6.4.5 p 5 from n1256.pdf. string literals are immutable and
              >"replaced" in TP 7 by static storage duration arrays. Their actual
              >type is char [N] where N is the integer returned by sizeof, or wchar_t
              >[N] (for wide string literals).
              >When evaluated, these become char * and wchar_t *.
              I forget how the standard characterized evaluated, but certainly
              in not all cases does it become pointers (for instance, sizeof).
              >Notice that GetString lies to the programmer; the returned value
              >cannot be modified! Unless you document that, your function is
              >misleading.
              And/or return const char * too.
              --
              Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
              Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
              World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
              Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

              Comment

              • Harald van =?UTF-8?b?RMSzaw==?=

                #8
                Re: Type of a string literal

                On Sat, 26 Jul 2008 11:04:26 -0400, Greg Comeau wrote:
                In article
                <174af926-0721-4b75-b2ab-353c6af58c98@m4 4g2000hsc.googl egroups.com>,
                <vippstar@gmail .comwrote:
                >>When evaluated, [string literals] become char * and wchar_t *.
                >
                I forget how the standard characterized evaluated, but certainly in not
                all cases does it become pointers (for instance, sizeof).
                The operand of sizeof is not usually evaluated.

                It's not about whether string literals are evaluated. As part of a non-
                evaluated expression, string literals and other arrays are normally still
                converted to pointers in at least one sense.

                if (0)
                puts("Hello, world!");

                Even though the literal will never be evaluated, the compiler should not
                complain about an invalid function argument type. In the other direction,
                as part of the & operator, string literals are evaluated, but not
                converted to pointers.

                Comment

                • Greg Comeau

                  #9
                  Re: Type of a string literal

                  In article <3493e$488b42aa $541dfcd3$7467@ cache4.tilbu1.n b.home.nl>,
                  Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                  >On Sat, 26 Jul 2008 11:04:26 -0400, Greg Comeau wrote:
                  >In article
                  ><174af926-0721-4b75-b2ab-353c6af58c98@m4 4g2000hsc.googl egroups.com>,
                  > <vippstar@gmail .comwrote:
                  >>>When evaluated, [string literals] become char * and wchar_t *.
                  >>
                  >I forget how the standard characterized evaluated, but certainly in not
                  >all cases does it become pointers (for instance, sizeof).
                  >
                  >The operand of sizeof is not usually evaluated.
                  >
                  >It's not about whether string literals are evaluated. As part of a non-
                  >evaluated expression, string literals and other arrays are normally still
                  >converted to pointers in at least one sense.
                  >
                  if (0)
                  puts("Hello, world!");
                  >
                  >Even though the literal will never be evaluated, the compiler should not
                  >complain about an invalid function argument type.
                  This is far from the original question, but where in the standard
                  does it say that the compiler should not complain? (I'm not saying
                  it doesn't say it, just never considered this possibility.)
                  In the other direction,
                  >as part of the & operator, string literals are evaluated, but not
                  >converted to pointers.
                  Which was part of my point though I mischaracterize d it.
                  --
                  Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
                  Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                  World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                  Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                  Comment

                  • Greg Comeau

                    #10
                    Re: Type of a string literal

                    In article <g6fgrr$m6$1@pa nix1.panix.com> ,
                    Greg Comeau <comeau@comeauc omputing.comwro te:
                    >In article <3493e$488b42aa $541dfcd3$7467@ cache4.tilbu1.n b.home.nl>,
                    >Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                    > if (0)
                    > puts("Hello, world!");
                    >>
                    >>Even though the literal will never be evaluated, the compiler should not
                    >>complain about an invalid function argument type.
                    >
                    >This is far from the original question, but where in the standard
                    >does it say that the compiler should not complain? (I'm not saying
                    >it doesn't say it, just never considered this possibility.)
                    Actually, I changed my mind and I will say it doesn't say it. :)
                    Either way I'm curious about whatever appropriate passage you
                    believe sheds light on this. Thanks.
                    --
                    Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
                    Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                    Comment

                    • Harald van =?UTF-8?b?RMSzaw==?=

                      #11
                      Re: Type of a string literal

                      On Sat, 26 Jul 2008 11:54:11 -0400, Greg Comeau wrote:
                      In article <g6fgrr$m6$1@pa nix1.panix.com> , Greg Comeau
                      <comeau@comeauc omputing.comwro te:
                      >>In article <3493e$488b42aa $541dfcd3$7467@ cache4.tilbu1.n b.home.nl>,
                      >>Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                      >> if (0)
                      >> puts("Hello, world!");
                      >>>
                      >>>Even though the literal will never be evaluated, the compiler should
                      >>>not complain about an invalid function argument type.
                      >>
                      >>This is far from the original question, but where in the standard does
                      >>it say that the compiler should not complain? (I'm not saying it
                      >>doesn't say it, just never considered this possibility.)
                      >
                      Actually, I changed my mind and I will say it doesn't say it. :) Either
                      way I'm curious about whatever appropriate passage you believe sheds
                      light on this. Thanks.
                      The code is valid, because even though the function is never called, the
                      array-to-pointer conversion ensures the constraint that a function's
                      arguments are assignment-compatible with its parameters is not violated.
                      I'm sure you're familiar with this, so I've not looked up where and how
                      exactly the standard specifies this. If a compiler wants to warn "function
                      argument of array type, expected pointer", but still continues to compile
                      the code, it can conform to the standard while relying on the as-if rule,
                      but it's a horrible idea. For that reason, I claim it should not complain,
                      but not that it must not complain.

                      Comment

                      • Greg Comeau

                        #12
                        Re: Type of a string literal

                        In article <e9d0b$488b4eb5 $541dfcd3$10754 @cache3.tilbu1. nb.home.nl>,
                        Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                        >On Sat, 26 Jul 2008 11:54:11 -0400, Greg Comeau wrote:
                        >In article <g6fgrr$m6$1@pa nix1.panix.com> , Greg Comeau
                        ><comeau@comeau computing.comwr ote:
                        >>>In article <3493e$488b42aa $541dfcd3$7467@ cache4.tilbu1.n b.home.nl>,
                        >>>Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                        >>> if (0)
                        >>> puts("Hello, world!");
                        >>>>
                        >>>>Even though the literal will never be evaluated, the compiler should
                        >>>>not complain about an invalid function argument type.
                        >>>
                        >>>This is far from the original question, but where in the standard does
                        >>>it say that the compiler should not complain? (I'm not saying it
                        >>>doesn't say it, just never considered this possibility.)
                        >>
                        >Actually, I changed my mind and I will say it doesn't say it. :) Either
                        >way I'm curious about whatever appropriate passage you believe sheds
                        >light on this. Thanks.
                        >
                        >The code is valid, because even though the function is never called, the
                        >array-to-pointer conversion ensures the constraint that a function's
                        >arguments are assignment-compatible with its parameters is not violated.
                        >I'm sure you're familiar with this, so I've not looked up where and how
                        >exactly the standard specifies this. If a compiler wants to warn "function
                        >argument of array type, expected pointer", but still continues to compile
                        >the code, it can conform to the standard while relying on the as-if rule,
                        >but it's a horrible idea. For that reason, I claim it should not complain,
                        >but not that it must not complain.
                        Actually, for some reason I saw fputs not puts, which now makes
                        this a different issue. Ok, so you're saying that because of the
                        if (0) that the puts call is not evaluated, right? If so, I don't
                        see how that trumps prototypes, syntax checking, etc.
                        --
                        Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
                        Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                        World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                        Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                        Comment

                        • santosh

                          #13
                          Re: Type of a string literal

                          Greg Comeau wrote:
                          In article <e9d0b$488b4eb5 $541dfcd3$10754 @cache3.tilbu1. nb.home.nl>,
                          Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                          >>On Sat, 26 Jul 2008 11:54:11 -0400, Greg Comeau wrote:
                          >>In article <g6fgrr$m6$1@pa nix1.panix.com> , Greg Comeau
                          >><comeau@comea ucomputing.comw rote:
                          >>>>In article <3493e$488b42aa $541dfcd3$7467@ cache4.tilbu1.n b.home.nl>,
                          >>>>Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                          >>>> if (0)
                          >>>> puts("Hello, world!");
                          >>>>>
                          >>>>>Even though the literal will never be evaluated, the compiler
                          >>>>>should not complain about an invalid function argument type.
                          >>>>
                          >>>>This is far from the original question, but where in the standard
                          >>>>does
                          >>>>it say that the compiler should not complain? (I'm not saying it
                          >>>>doesn't say it, just never considered this possibility.)
                          >>>
                          >>Actually, I changed my mind and I will say it doesn't say it. :)
                          >>Either way I'm curious about whatever appropriate passage you
                          >>believe sheds
                          >>light on this. Thanks.
                          >>
                          >>The code is valid, because even though the function is never called,
                          >>the array-to-pointer conversion ensures the constraint that a
                          >>function's arguments are assignment-compatible with its parameters is
                          >>not violated. I'm sure you're familiar with this, so I've not looked
                          >>up where and how exactly the standard specifies this. If a compiler
                          >>wants to warn "function argument of array type, expected pointer", but
                          >>still continues to compile the code, it can conform to the standard
                          >>while relying on the as-if rule, but it's a horrible idea. For that
                          >>reason, I claim it should not complain, but not that it must not
                          >>complain.
                          >
                          Actually, for some reason I saw fputs not puts, which now makes
                          this a different issue. Ok, so you're saying that because of the
                          if (0) that the puts call is not evaluated, right? If so, I don't
                          see how that trumps prototypes, syntax checking, etc.
                          I suppose the translation phases that do those things never get to see
                          the function in question.

                          Comment

                          • Greg Comeau

                            #14
                            Re: Type of a string literal

                            In article <g6fkbo$hrb$1@r egistered.motza rella.org>,
                            santosh <santosh.k83@gm ail.comwrote:
                            >Greg Comeau wrote:
                            >In article <e9d0b$488b4eb5 $541dfcd3$10754 @cache3.tilbu1. nb.home.nl>,
                            >Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                            >>>On Sat, 26 Jul 2008 11:54:11 -0400, Greg Comeau wrote:
                            >>>In article <g6fgrr$m6$1@pa nix1.panix.com> , Greg Comeau
                            >>><comeau@come aucomputing.com wrote:
                            >>>>>In article <3493e$488b42aa $541dfcd3$7467@ cache4.tilbu1.n b.home.nl>,
                            >>>>>Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                            >>>>> if (0)
                            >>>>> puts("Hello, world!");
                            >>>>>>
                            >>>>>>Even though the literal will never be evaluated, the compiler
                            >>>>>>should not complain about an invalid function argument type.
                            >>>>>
                            >>>>>This is far from the original question, but where in the standard
                            >>>>>does
                            >>>>>it say that the compiler should not complain? (I'm not saying it
                            >>>>>doesn't say it, just never considered this possibility.)
                            >>>>
                            >>>Actually, I changed my mind and I will say it doesn't say it. :)
                            >>>Either way I'm curious about whatever appropriate passage you
                            >>>believe sheds
                            >>>light on this. Thanks.
                            >>>
                            >>>The code is valid, because even though the function is never called,
                            >>>the array-to-pointer conversion ensures the constraint that a
                            >>>function's arguments are assignment-compatible with its parameters is
                            >>>not violated. I'm sure you're familiar with this, so I've not looked
                            >>>up where and how exactly the standard specifies this. If a compiler
                            >>>wants to warn "function argument of array type, expected pointer", but
                            >>>still continues to compile the code, it can conform to the standard
                            >>>while relying on the as-if rule, but it's a horrible idea. For that
                            >>>reason, I claim it should not complain, but not that it must not
                            >>>complain.
                            >>
                            >Actually, for some reason I saw fputs not puts, which now makes
                            >this a different issue. Ok, so you're saying that because of the
                            >if (0) that the puts call is not evaluated, right? If so, I don't
                            >see how that trumps prototypes, syntax checking, etc.
                            >
                            >I suppose the translation phases that do those things never get to see
                            >the function in question.
                            Which returns me asking (though now for a different reason)
                            where that is said in the standard.
                            --
                            Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
                            Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                            World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                            Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                            Comment

                            • Harald van =?UTF-8?b?RMSzaw==?=

                              #15
                              Re: Type of a string literal

                              On Sat, 26 Jul 2008 12:41:57 -0400, Greg Comeau wrote:
                              >>>>In article <3493e$488b42aa $541dfcd3$7467@ cache4.tilbu1.n b.home.nl>,
                              >>>>Harald van =?UTF-8?b?RMSzaw==?= <truedfx@gmail. comwrote:
                              >>>> if (0)
                              >>>> puts("Hello, world!");
                              >>>>>
                              >>>>>Even though the literal will never be evaluated, the compiler should
                              >>>>>not complain about an invalid function argument type.
                              [snip]
                              Ok, so you're saying that because of the if (0) that
                              the puts call is not evaluated, right?
                              Right.
                              If so, I don't see how that
                              trumps prototypes, syntax checking, etc.
                              It doesn't. That's my point. The original claim was "When evaluated,
                              [string literals] become char * and wchar_t *." I am trying to say that
                              they also become char * and wchar_t *, at least in one sense, when not
                              evaluated, because that's the only way a compiler can sanely accept the
                              code.

                              Comment

                              Working...