Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #120443 > unrolled thread

literal string type

Started byThiago Adams <thiago.adams@gmail.com>
First post2017-09-28 07:41 -0700
Last post2017-09-28 11:41 -0700
Articles 7 — 4 participants

Back to article view | Back to comp.lang.c


Contents

  literal string type Thiago Adams <thiago.adams@gmail.com> - 2017-09-28 07:41 -0700
    Re: literal string type Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-09-28 07:50 -0700
      Re: literal string type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-09-28 19:12 +0100
    Re: literal string type Keith Thompson <kst-u@mib.org> - 2017-09-28 09:08 -0700
      Re: literal string type Thiago Adams <thiago.adams@gmail.com> - 2017-09-28 10:19 -0700
        Re: literal string type Keith Thompson <kst-u@mib.org> - 2017-09-28 11:06 -0700
          Re: literal string type Thiago Adams <thiago.adams@gmail.com> - 2017-09-28 11:41 -0700

#120443 — literal string type

FromThiago Adams <thiago.adams@gmail.com>
Date2017-09-28 07:41 -0700
Subjectliteral string type
Message-ID<150a364a-68b1-4c35-9d34-e94bbf115d23@googlegroups.com>
This sample:

void F(char * s) {
}

int main() {
    F("a");
    return 0;
}

Compiles with zero warnings.

But this one:

void F(char * s) {}

int main()
{
    const char s[2] = "a";
    F(s);
    
    return 0;
}

give us:

main.c:7:7: warning: passing argument 1 of ‘F’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
     F(s);
       ^

Any ideas why the first sample doesn't have warnings?
I have seen this in more than one compiler.

[toc] | [next] | [standalone]


#120444

FromMalcolm McLean <malcolm.arthur.mclean@gmail.com>
Date2017-09-28 07:50 -0700
Message-ID<c282478b-c968-4eb6-8b7a-d9b5b7b4a527@googlegroups.com>
In reply to#120443
On Thursday, September 28, 2017 at 3:41:46 PM UTC+1, Thiago Adams wrote:
> This sample:
> 
> void F(char * s) {
> }
> 
> int main() {
>     F("a");
>     return 0;
> }
> 
> Compiles with zero warnings.
> 
> But this one:
> 
> void F(char * s) {}
> 
> int main()
> {
>     const char s[2] = "a";
>     F(s);
>     
>     return 0;
> }
> 
> give us:
> 
> main.c:7:7: warning: passing argument 1 of ‘F’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
>      F(s);
>        ^
> 
> Any ideas why the first sample doesn't have warnings?
> I have seen this in more than one compiler.
>
A string literal has type non-const char * despite being read only, as a special
exceptional rule designed to avoid too many breaking changes to pre-const code.

[toc] | [prev] | [next] | [standalone]


#120469

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-09-28 19:12 +0100
Message-ID<87vak2g09w.fsf@bsb.me.uk>
In reply to#120444
Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
<snip>
> A string literal has type non-const char *

Strictly speaking they have array type, not pointer type.  Expressions
with array type convert, in almost all cases, into pointers so you can
only tell the difference in a few special cases.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#120449

FromKeith Thompson <kst-u@mib.org>
Date2017-09-28 09:08 -0700
Message-ID<lnefqqyfdx.fsf@kst-u.example.com>
In reply to#120443
Thiago Adams <thiago.adams@gmail.com> writes:
> This sample:
>
> void F(char * s) {
> }
>
> int main() {
>     F("a");
>     return 0;
> }
> 
> Compiles with zero warnings.

("int main(void)" is preferred.)

The string literal "a" is of type `char [2]`, which is implicitly
converted to `char*`.  Logically it would be better for it to be of type
`const char[2]`, which would be converted to `const char*`, but it
remains non-const for compatility with pre-ANSI C code (when "const"
didn't exist).  (C++ did make this change; a C++ compiler would
diagnose, and perhaps reject, the above code.)

> But this one:
>
> void F(char * s) {}
>
> int main()
> {
>     const char s[2] = "a";
>     F(s);
>     
>     return 0;
> }
>
> give us:
>
> main.c:7:7: warning: passing argument 1 of ‘F’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
>      F(s);
>        ^

"a" is still of type `char[2]`, and it's used to initialze an object s
of type `const char[2]`, which converts to `const char*`.  By not using
"const" on its parameter declaration, F() threatens to modify the data
pointed to by the argument.

> Any ideas why the first sample doesn't have warnings?
> I have seen this in more than one compiler.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#120462

FromThiago Adams <thiago.adams@gmail.com>
Date2017-09-28 10:19 -0700
Message-ID<ba150c26-406d-494f-993e-898d43feda63@googlegroups.com>
In reply to#120449
On Thursday, September 28, 2017 at 1:09:06 PM UTC-3, Keith Thompson wrote:
...
> The string literal "a" is of type `char [2]`, which is implicitly
> converted to `char*`.  Logically it would be better for it to be of type
> `const char[2]`, which would be converted to `const char*`, but it
> remains non-const for compatility with pre-ANSI C code (when "const"
> didn't exist).  (C++ did make this change; a C++ compiler would
> diagnose, and perhaps reject, the above code.)

In my option, it's a good warning to have, and a good point for
deprecation in the future.

[toc] | [prev] | [next] | [standalone]


#120467

FromKeith Thompson <kst-u@mib.org>
Date2017-09-28 11:06 -0700
Message-ID<lnwp4iwvdl.fsf@kst-u.example.com>
In reply to#120462
Thiago Adams <thiago.adams@gmail.com> writes:
> On Thursday, September 28, 2017 at 1:09:06 PM UTC-3, Keith Thompson wrote:
> ...
>> The string literal "a" is of type `char [2]`, which is implicitly
>> converted to `char*`.  Logically it would be better for it to be of type
>> `const char[2]`, which would be converted to `const char*`, but it
>> remains non-const for compatility with pre-ANSI C code (when "const"
>> didn't exist).  (C++ did make this change; a C++ compiler would
>> diagnose, and perhaps reject, the above code.)
>
> In my option, it's a good warning to have, and a good point for
> deprecation in the future.

gcc's "-Wwrite-strings" option makes string literals const (which makes
gcc non-conforming).  Other compilers may have similar options.
(Sometimes compiling with a C++ compiler can detect some errors that C
compilers don't -- but of course not all valid C code is valid C++
code.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#120473

FromThiago Adams <thiago.adams@gmail.com>
Date2017-09-28 11:41 -0700
Message-ID<fd78c8c8-695e-4382-a4f8-565b5c478259@googlegroups.com>
In reply to#120467
On Thursday, September 28, 2017 at 3:06:38 PM UTC-3, Keith Thompson wrote:
...
> gcc's "-Wwrite-strings" option makes string literals const (which makes
> gcc non-conforming).  Other compilers may have similar options.

I found for Microsoft compiler:

/Zc:strictStrings (Disable string literal type conversion)

https://msdn.microsoft.com/en-us/library/dn449508.aspx

It worked just for C++.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web