473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2199
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
14
by: LBJ | last post by:
I declare pointers as follows: const int* pData; //pointer to read only data int* const pData2; //read only pointer Then I make the following assignment: pData = pData2; the read-only...
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
1
by: qwerty2_reverse_iterator | last post by:
Is this a bug with the ms compiler (V7.1)? (It seems so at least.) I get errors when I don't initialize all the const pointer fields of an anonymous union in a struct. Example: //T2.h...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: junky_fellow | last post by:
Hi guys, Consider the following statements. const int *ptr; /* ie the contents pointed to by ptr cannot be changed */ My question is how/who prevents the contents from being modified ?...
6
by: subramanian | last post by:
Consider the following program: #include <stdio.h> void myfn(const int **a) { static int i, j, k; a = &i; a = &j;
2
by: subramanian100in | last post by:
Consider the following program: #include <stdio.h> void myfn(const int **a) { static int i, j, k; a = &i; a = &j;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.