468,785 Members | 1,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

Is `removal of const qualifier from target pointer' a warning or an error?

Gcc only gives out a warning: `assignment discards qualifiers from
pointer target type' against code such as following:
$ type a.c
int main(void)
{
const char *pc;
char *p = pc;

return 0;
}

$ gcc -std=c99 -W -Wall a.c
a.c: In function `main':
a.c:4: warning: initialization discards qualifiers from pointer target
type
a.c:4: warning: unused variable `p'

$
`C: A Reference Manual, 5th', sec. 5.11.1 states that: `... In
standard C, the presence of any type qualifiers changes the
type: ...'. It says in sec 5.11.6: `Two (similarly qualified) pointer
types are compatible if they point to compatible types.'

In the above code snippet, `const char' and `char' are not qualified
similarly, they're not compatible types. pc and p are not compatible
pointers for they point to incompatible types. So, why isn't the
initialization above an error?

Oct 27 '07 #1
4 2000
lovecreatesbea...@gmail.com wrote:
Gcc only gives out a warning: `assignment discards qualifiers from
pointer target type' against code such as following:
$ type a.c
int main(void)
{
const char *pc;
char *p = pc;

return 0;
}

$ gcc -std=c99 -W -Wall a.c
a.c: In function `main':
a.c:4: warning: initialization discards qualifiers from pointer target
type
a.c:4: warning: unused variable `p'

$
`C: A Reference Manual, 5th', sec. 5.11.1 states that: `... In
standard C, the presence of any type qualifiers changes the
type: ...'. It says in sec 5.11.6: `Two (similarly qualified) pointer
types are compatible if they point to compatible types.'

In the above code snippet, `const char' and `char' are not qualified
similarly, they're not compatible types. pc and p are not compatible
pointers for they point to incompatible types. So, why isn't the
initialization above an error?
pc and p do point to compatible types.

Oct 27 '07 #2
santosh wrote:
lovecreatesbea...@gmail.com wrote:
....
>$ type a.c
int main(void)
{
const char *pc;
char *p = pc;

return 0;
}
....
pc and p do point to compatible types.
Citation, please? Here's mine:

6.2.5p26:
The qualified or unqualified
versions of a type are distinct types
6.7.3p9 says:
For two qualified types to be compatible, both shall have the identically qualified version
of a compatible type;
Oct 27 '07 #3
lovecreatesbea...@gmail.com wrote:
Gcc only gives out a warning: `assignment discards qualifiers from
pointer target type' against code such as following:
$ type a.c
int main(void)
{
const char *pc;
char *p = pc;

return 0;
}

$ gcc -std=c99 -W -Wall a.c
a.c: In function `main':
a.c:4: warning: initialization discards qualifiers from pointer target
type
a.c:4: warning: unused variable `p'

$
`C: A Reference Manual, 5th', sec. 5.11.1 states that: `... In
standard C, the presence of any type qualifiers changes the
type: ...'. It says in sec 5.11.6: `Two (similarly qualified) pointer
types are compatible if they point to compatible types.'

In the above code snippet, `const char' and `char' are not qualified
similarly, they're not compatible types. pc and p are not compatible
pointers for they point to incompatible types. So, why isn't the
initialization above an error?
The relevant rules do not simply require that the two types be
compatible. It's a bit more complicated than that.

The relevant constraint is 6.5.16.1p1, which requires that for simple
assignment "both operands are pointers to qualified or unqualified
versions of compatible types, and the type pointed to by the left has
all the qualifiers of the type pointed to by the right;" In other words,
you cannot discard qualifiers from the pointed-at type during
assignment, but you can add them.

This is an initialization rather than an assignment, but the rules for
initializations cross-reference the rules for assignment in this
context (6.7.8p11).

Therefore, this code does contain a constraint violation, just not the
one you thought it had. Why gcc makes this a warning rather than an
error is a matter you should take up with the implementors of gcc. The
standard merely requires that there be at least one diagnostic message
for a constraint violation, and a warning message counts just as well as
an error message. The standard does not require that the program be
rejected.
Oct 27 '07 #4
On Sat, 27 Oct 2007 14:40:13 +0000, lovecreatesbea...@gmail.com wrote:
Gcc only gives out a warning: `assignment discards qualifiers from
pointer target type' against code such as following:
[...] So, why isn't the
initialization above an error?
The standard never *requires* an implementation to fail
translating a program, except when it contains a #error directive.
The behavior of a program with a constraint violation which
happens to compile anyway is undefined by omission, but I would be
very surprised if it was any different from a program with
char *p = (char *)pc;
In the very code you posted, pc is indeterminate, but I assume
in the real code it points to somewhere or is a null pointer. If
the object it points is itself const, I'd declare p as a
const char * as well.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 27 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by LBJ | last post: by
6 posts views Thread by bob_jenkins | last post: by
1 post views Thread by qwerty2_reverse_iterator | last post: by
4 posts views Thread by junky_fellow | last post: by
6 posts views Thread by subramanian | last post: by
2 posts views Thread by subramanian100in | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.