Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: Is a char array initializer a "string literal"? Date: Tue, 26 Jul 2022 12:13:19 -0700 Organization: None to speak of Lines: 64 Message-ID: <87o7xby9f4.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="4ccf06c8faeecc2e9e6925153f25a68d"; logging-data="2249126"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19eDPKNsS4vGvjQxwiBApgP" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:C3ItOhvbDqjcW8+LXs64okaS5B8= sha1:YtY+POpDDkVQ9OIMDVpicOLaSGI= Xref: csiph.com comp.lang.c++:85669 Juha Nieminen writes: > In another forum I was discussing string literals and char arrays. > During the conversation this question occurred to me: > In an array initialization like this: > > char str[] = "hello"; > > can that "hello" even be considered a "string literal", or should it > be classified as something else? It's a string literal. A string literal is a syntactic construct, a sequence of characters in a source file. "hello" unquestionably meets the definition of *string-literal* given in the C++ grammar. A char array is an object that can exist during program execution. > The question arose because an (actual) string literal has a type of > array-of-const-char (of a size needed to store the contents of the > string literal). So for example the type of "hello" is const char[6]. Yes. > However, you can't initialize an array with another array. Given that > fact, that would mean that the "hello" in > > char str[] = "hello"; > > is not a const char[6] (because you can't initialize 'str' with one). There's a specific rule that allows a char array to be initialized with a string literal. See [dcl.init.string] in the standard. The string literal "hello" is always of type `const char[6]`. In most contexts, it "decays" to a pointer expression of type `const char*` which evaluates to the address of the initial character of the array. A string literal used as an initializer for a character array object is one of the exceptions, where it does not "decay". The array value is used to initialize the array object. (Another exception is the operand of `sizeof`.) > It's something else. In fact, the above is essentially just syntactic > sugar for: > > char str[] = { 'h', 'e', 'l', 'l', 'o', '\0' }; > > Thus, should that "hello", in this context, be considered an initializer > list rather than a "string literal"? No, it's a string literal. The fact that it happens to be equivalent to some other syntactic construct does not change that fact. > (Yes, I wouldn't be surprised if the standard still calls it a "string > literal". But even standards are flawed and ambiguous sometimes. No > standard is absolutely perfect.) Yes, standards are flawed and ambiguous, but this is not a flaw or an ambiguity. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */