[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    Re: [PATCH] ed: add support for -p command-line option as mandated by POSIX
From:       Harald van Dijk <harald () gigawatt ! nl>
Date:       2021-11-21 15:22:39
Message-ID: 68669e4c-36fd-c0bc-cc03-b6eeab79be89 () gigawatt ! nl
[Download RAW message or body]

On 21/11/2021 14:52, Kang-Che Sung wrote:
> On Sunday, November 21, 2021, Harald van Dijk <harald@gigawatt.nl 
> <mailto:harald@gigawatt.nl>> wrote:
>  > On 21/11/2021 07:12, Kang-Che Sung wrote:
>  >> I don't know why you are messing up with the "constness" of the 
> strings. C standard says string literal is of type const char[], and the 
> const keyword didn't exist before C89.
>  >> Note the compiler is free to merge string literals with identical 
> content so they share the same buffer in the .rodata section (that's why 
> they are const).
>  >
>  > It does not say that, it says the opposite:
>  >
>  >   The multibyte character sequence is then used to initialize an array
>  >   of static storage duration and length just sufficient to contain the
>  >   sequence. For character string literals, the array elements have type
>  >   char, and are initialized with the individual bytes of the multibyte
>  >   character sequence.
>  >
>  > Note the "char" as opposed to "const char".
> 
> It refers to the initialization like this:
> 
>     char str[] = "abc";
>     str[0] = 'd'; // Vaild

No, it doesn't. I was quoting 6.4.5 String literals, which covers string 
literals. Initialisation is covered by 6.7.9 Initialization, reading

   An array of character type may be initialized by a character string
   literal or UTF−8 string literal, optionally enclosed in braces.
   Successive bytes of the string literal (including the terminating null
   character if there is room or if the array is of unknown size)
   initialize the elements of the array.

But maybe you would like something more explicit: that section also 
contains in an example:

   On the other hand, the declaration

     char *p = "abc";

   defines p with  type "pointer to char" and initializes it to point to
   an object with type "array of char" with length 4 whose elements are
   initialized with a character string literal. If an attempt is made to
   use p to modify the contents of the array, the behavior is undefined.

It explicitly says the string literal itself has type "array of char", 
not "array of const char". Modifying it is undefined, but that does not 
make the type const.

Except, as noted, in the non-standard mode in which the compiler is 
invoked for busybox.

Cheers,
Harald van Dijk
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic