473,748 Members | 7,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2229
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
3638
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
14
3282
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 aspect of pData2 is not in any danger of being
6
8259
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 generic pointer type. My real question is, is (void *) such a generic pointer type that it
1
2402
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 typedef int Type1; typedef float Type2; struct S1 {
10
2789
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1875
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, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
4
1672
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 ? Is it the C compiler that would give compile time error while trying to change the contents ? Or is it the implementation that would somehow
6
2109
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
1715
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
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9319
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8241
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.