473,625 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

conversion from const type* to type* allowed?

The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Jan 4 '07 #1
13 1471
Michal Nazarewicz wrote:
The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?
Const is broken in C, that's why one shouldn't ignore compiler warnings!

--
Ian Collins.
Jan 4 '07 #2
Michal Nazarewicz wrote On 01/04/07 15:55,:
The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?
From back to front: It is not allowed, GCC is not buggy
(not in this regard, anyway), and you are in fact missing
something. What you're missing is that the C Standard makes
no distinction between error messages, warning messages,
informative messages, and spirit messages from the Great
Beyond: They are all merely "diagnostic s." Once the compiler
has emitted a "diagnostic " for the erroneous construct, it
is under no obligation to halt compilation or reject the
program[*], it may (if it feels like it) emit the diagnostic
and translate the program anyhow.

There's a GCC option ("-Werror"?) that turns "warnings"
into "fatal errors" if you prefer.
[*] Exception: The compiler is required to reject a
module containing a non-suppressed #error directive.
--
Er*********@sun .com
Jan 4 '07 #3
Michal Nazarewicz <mi****@tlen.pl writes:
The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?
The implicit conversion is a constraint violation. A conforming
compiler's only obligation in the presence of a constraint violation
is to issue a diagnostic. gcc meets this requirement by printing a
warning.

The only case where a compiler is actually required to reject a
translation unit is when it contains a "#error" directive.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 4 '07 #4
On Thu, 04 Jan 2007 21:55:28 +0100, Michal Nazarewicz <mi****@tlen.pl >
wrote:
>The following code compiles fine (with warnings though)
If it had warnings, it did not compile fine. Just remember that the
compiler is actually too dumb to know the difference between a serious
error and a trivial one, though it like to pretend it does.

Strive to have all your programs compile cleanly without errors or
warnings. There are cases where warnings can be ignored*, but only if
you understand exactly what caused them and know for a fact that it
won't be a problem.

*One which comes to mind is an HP-UX warning that is issued whenever
you use "const". It just tells you that using const improperly is bad,
without knowing whether your usage was bad or not <g>.
under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?
--
Al Balmer
Sun City, AZ
Jan 4 '07 #5
Al Balmer said:

<snip>
Strive to have all your programs compile cleanly without errors or
warnings. There are cases where warnings can be ignored*, but only if
you understand exactly what caused them and know for a fact that it
won't be a problem.
Perhaps "overruled" would be a better word than "ignored". When gcc tells me
that I've only partly initialised an aggregate object - e.g. mystruct s =
{0}; - I take due note of the warning, and check that the initialisation is
correct. Actually, this is such a frequent occurrence that I suppose
"ignored" is pretty accurate in that case, so let me find a different
example (taken from real code this time):

Dest->n[i] = in & 0xff;

Dest->n[i] has type unsigned char. in is a long int (which, incidentally, is
known to be non-negative by this stage in the proceedings). By the rules of
C, in & 0xff must must must must must be in the range of unsigned char, so
I know for sure that it will fit into Dest->n[i] - and yet one of my
compilers issues a diagnostic message saying that information might be lost
in this assignment. It can't possibly be - but the compiler isn't quite
bright enough to work this out, so it warns me.

And it's a reasonable warning, on the whole. So I let the compiler warn me,
and I look at the warning, and I check that it really is okay. If it is, I
may add a comment to the code to save me a little time next time through,
but by paying attention to the warning at least the first time it appears,
I have given myself the opportunity to check out a potential bug.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 4 '07 #6
On Thu, 04 Jan 2007 21:57:24 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
>Al Balmer said:

<snip>
>Strive to have all your programs compile cleanly without errors or
warnings. There are cases where warnings can be ignored*, but only if
you understand exactly what caused them and know for a fact that it
won't be a problem.

Perhaps "overruled" would be a better word than "ignored".
I agree. I'll remember that the next time I give that speech :-)

--
Al Balmer
Sun City, AZ
Jan 4 '07 #7
Eric Sosman wrote:
Michal Nazarewicz wrote On 01/04/07 15:55,:
The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:
<snip>
... The compiler is required to reject a
module containing a non-suppressed #error directive.
That is true of C99, but rejection under #error is not a requirement of
C90
implementations . However, the C90 implementation the OP is using will.

--
Peter

Jan 5 '07 #8
Al Balmer <al******@att.n etwrites:
On Thu, 04 Jan 2007 21:55:28 +0100, Michal Nazarewicz <mi****@tlen.pl >
wrote:
>>The following code compiles fine (with warnings though)

If it had warnings, it did not compile fine.
True and I always check all the warnings compiler gives me but at this
point I was shocked that compiler generated an executable where
implicit conversion from pointer to const type to pointer to type was
done.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Jan 5 '07 #9
Eric Sosman <Er*********@su n.comwrites:
Michal Nazarewicz wrote On 01/04/07 15:55,:
>The following code compiles fine (with warnings though) under GCC with
-pedantic and -ansi arguments:

#v+
int main(void) {
const int a = 0;
int *p = &a;
return 0;
}
#v-

so am I missing something, GCC is buggy or it is really allowed to
convert a pointer to const type to pointer to type?

From back to front: It is not allowed, GCC is not buggy
(not in this regard, anyway), and you are in fact missing
something. What you're missing is that the C Standard makes
no distinction between error messages, warning messages,
informative messages, and spirit messages from the Great
Beyond: They are all merely "diagnostic s." Once the compiler
has emitted a "diagnostic " for the erroneous construct, it
is under no obligation to halt compilation or reject the
program[*], it may (if it feels like it) emit the diagnostic
and translate the program anyhow.
Thanks for explanation, I wasn't aware of such behaviour even though
it sounds rather strange to me.
There's a GCC option ("-Werror"?) that turns "warnings"
into "fatal errors" if you prefer.
That'd be too much. I use a -Wpadded switch which produces warnings
that some of them are to be ignored.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Jan 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1318
by: Tao Lu | last post by:
class A { pub: A(const char*); //1 A(char *); //2 } ; if i do A a("blah"); which constructor should be called?
14
1709
by: ES Kim | last post by:
Consider: #include <string> #include <iostream> using namespace std; struct S { const char* ps_; operator string();
9
2365
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want people to be able to initialize real numbers (like an object of type double) from it ... in such a context (or if someone explicitly casts to double), the imaginary part is to be ignored. However, I certainly do not want people to be able to ask...
15
2573
by: Alexander Stippler | last post by:
hi, the following does not work. I do not understand why, since it works if I replace line marked by (1) with the line below ((2)). Compiling with gcc4.0 results in "error: conversion from 'Two<int>' to non-scalar type 'One<int>' requested". Has somebody an explanation for me? template <typename T> struct Convert
7
3256
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But I couldn't find the exact rule in the C++ standard. And what if the classes have template parameters? It would be great if somebody could get me a rough hint where in the
3
3902
by: Alexander Stippler | last post by:
Given the following code snippet we get some unexpected behaviour: //-------------------------------------------------------------------- #include <iostream> using namespace std; struct A { A() { cerr << "A()" << endl; } };
4
2901
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to SignalMask:s. The reason is I have a free function called WaitSignal that accepts av SignalMask where Signals parameters are supposed to implicitly be converted to SignalMask:s. I'm using the SignalMask class because I want to be able to supply a logic...
6
2337
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
6
2628
by: Rahul | last post by:
Hi Everyone, I have the following code, class B; class A { public : operator B();
0
8259
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
8192
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
8358
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
7188
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...
1
6119
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
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
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
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
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.