This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: aliasing between const and non-const objects
- From: Michael Matz <matz at suse dot de>
- To: Richard dot Earnshaw at arm dot com
- Cc: gcc at gcc dot gnu dot org
- Date: Tue, 29 Jul 2003 19:44:02 +0200 (CEST)
- Subject: Re: aliasing between const and non-const objects
Hi,
On Tue, 29 Jul 2003, Richard Earnshaw wrote:
> > You'll get a warning for the latter assignment, though. Nevethess you are
> > right. I should have provided a more complete example:
>
> I hope not. Otherwise you would get a warning every time you passed a
> non-const char pointer to strcpy as a source argument.
That the other way around. There you add the const qualifier, but
removing it (implicitely or explicitely) gives a warning:
const char *cp;
char *ncp;
void g(char *p);
void f()
{
char *lncp = cp;
ncp = cp;
g(cp);
}
ctonc.c: In function `f':
ctonc.c:6: warning: initialization discards qualifiers from pointer target type
ctonc.c:7: warning: assignment discards qualifiers from pointer target type
ctonc.c:8: warning: passing arg 1 of `g' discards qualifiers from pointer target type
> The code is not much dissimilar to
>
> char a[5] = "abc";
> char b[5];
>
> strcpy (b, a);
this is a char* --> const char* conversion which is okay.
Ciao,
Michael.