473,378 Members | 1,119 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,378 software developers and data experts.

const keyword

I have my TC++ help file which has the following in it:

const my_age = 39;
___________________________
WARNING: A const variable can be indirectly modified by a
pointer, as in
the following:

*(int *)&my_age = 35;

When the const modifier is used with a pointer parameter in
a function's
parameter list, the function cannot modify the variable
that the pointer
points to. For example,

int printf (const char *format, ...);

Here the printf function is prevented from modifying the
format string.
_________________

I can't figure out what they are trying to say.
Could someone help?

TIA.

--
main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
Jul 19 '05 #1
5 2527
On Sun, 12 Oct 2003 23:01:12 +0530, Ravi wrote:
I have my TC++ help file which has the following in it:

const my_age = 39;
___________________________
WARNING: A const variable can be indirectly modified by a
pointer, as in
the following:

*(int *)&my_age = 35;
They're saying that you can cast away const. This might or might not work
all the time (it'll compile, but it might fail in strange ways at
runtime). If your compiler documentation says you can do it, though, it
should be safe enough with that compiler.
When the const modifier is used with a pointer parameter in
a function's
parameter list, the function cannot modify the variable
that the pointer
points to. For example,

int printf (const char *format, ...);

Here the printf function is prevented from modifying the
format string.


They're saying that the string (the object format points to) is what's
const, not format itself.

int const* p; // *p is const
int* const p; // p is const

Josh
Jul 19 '05 #2
Josh Sebastian writes:
On Sun, 12 Oct 2003 23:01:12 +0530, Ravi wrote:
I have my TC++ help file which has the following in it:

const my_age = 39;
___________________________
WARNING: A const variable can be indirectly modified by a
pointer, as in
the following:

*(int *)&my_age = 35;


They're saying that you can cast away const. This might or might not work
all the time (it'll compile, but it might fail in strange ways at
runtime). If your compiler documentation says you can do it, though, it
should be safe enough with that compiler.


With my compiler: no errors, no warnings, no work. The const was not
changed.

Borland TC++W 3.1
Jul 19 '05 #3
On Sun, 12 Oct 2003 13:34:08 -0700, "osmium"
<r1********@comcast.net> wrote:
With my compiler: no errors, no warnings, no work. The const was not
changed.

Borland TC++W 3.1


Same here. But then why did they write that in the docs?

--
main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
Jul 19 '05 #4
osmium wrote:
Josh Sebastian writes:
On Sun, 12 Oct 2003 23:01:12 +0530, Ravi wrote:
> I have my TC++ help file which has the following in it:
>
> const my_age = 39;
> ___________________________
> WARNING: A const variable can be indirectly modified by a
> pointer, as in
> the following:
>
> *(int *)&my_age = 35;


They're saying that you can cast away const. This might or might not
work all the time (it'll compile, but it might fail in strange ways
at runtime). If your compiler documentation says you can do it,
though, it should be safe enough with that compiler.


With my compiler: no errors, no warnings, no work. The const was not
changed.


That's the reason why you should avoid C style casts. You tell the
compiler to be silent and do whatever it takes to convert to the
specified type. You basically say "shut up, I know this doesn't make
sense, but I want it anyway coz I know what I'm doing". Of course you
_must_ actually know what you're doing.

Jul 19 '05 #5
Rolf Magnus <ra******@t-online.de> writes:
osmium wrote:
Josh Sebastian writes:
On Sun, 12 Oct 2003 23:01:12 +0530, Ravi wrote:

> I have my TC++ help file which has the following in it:
>
> const my_age = 39;
> ___________________________
> WARNING: A const variable can be indirectly modified by a
> pointer, as in
> the following:
>
> *(int *)&my_age = 35;

They're saying that you can cast away const. This might or might not
work all the time (it'll compile, but it might fail in strange ways
at runtime). If your compiler documentation says you can do it,
though, it should be safe enough with that compiler.
With my compiler: no errors, no warnings, no work. The const was not
changed.


Perhaps the documentation is outdated; or perhaps they merely
wanted to point out that it *might* work when executed on other
platforms (of course, it might induce nasal daemons to fly out
your nose, too ;-) )
That's the reason why you should avoid C style casts. You tell the
compiler to be silent and do whatever it takes to convert to the
specified type. You basically say "shut up, I know this doesn't make
sense, but I want it anyway coz I know what I'm doing". Of course you
_must_ actually know what you're doing.


How would that be any less true using const_cast<> ?

-Micah
Jul 19 '05 #6

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

Similar topics

2
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
17
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
1
by: yatheeshgd | last post by:
Hello Guys!! Could you please tell me what is the difference in giving "const" keyword either before or after the function eg: const int f(); int f() const; const int f() const: ...
9
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int...
26
by: karthikbalaguru | last post by:
Hi, While trying to understand the difference between the following 2 methods, i have some interesting queries. Method 1) char *s = "Hello"; and Method 2) char s = "Hello"; How does the...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
29
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment:...
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.