473,385 Members | 2,003 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,385 software developers and data experts.

const int * assignment

hello,
const int *ptr1 mean i can change value of ptr1 but not of *ptr1
right? then why following snippet doesnot give error?
int val=50;
const int *ptr1=&val;
*(int *)ptr1=98; //what is this casting?
printf("\n%d %d",++val,*ptr1); //99 98 is output

regards,
rahul

Nov 15 '05 #1
7 2044
ra*******@gmail.com wrote:
hello,
const int *ptr1 mean i can change value of ptr1 but not of *ptr1
right? then why following snippet doesnot give error?
int val=50;
const int *ptr1=&val;
*(int *)ptr1=98; //what is this casting?
printf("\n%d %d",++val,*ptr1); //99 98 is output

regards,
rahul


This is due to the type cast which drops the constness of ptr1 (correct
me if I am wrong, I try getting into C99 myself for now).
Nov 15 '05 #2
<ra*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
hello,
const int *ptr1 mean i can change value of ptr1 but not of *ptr1
right? then why following snippet doesnot give error?
Because as you've already noted, there's a cast in the code below:
int val=50;
const int *ptr1=&val;
*(int *)ptr1=98; //what is this casting?


You'd have a compilation error if you tried assigning 98 to *ptr1 w/o any
cast.
But if I saw something like this in the code, I'd reconsider using it at
first place as it may be full of hidden bugs... Or if this is your code, If
I were you, I'd first make sure I understand what such and such C construct
means and does and only then I'd use it, whenever necessary. Simply put, the
above doesn't make mich sense.

Alex
Nov 15 '05 #3
ra*******@gmail.com wrote:
hello,
const int *ptr1 mean i can change value of ptr1 but not of
*ptr1 right?
Yes.
then why following snippet doesnot give error?
int val=50;
const int *ptr1=&val;
*(int *)ptr1=98; //what is this casting?
Because here you've overridden the nature of what ptr1 points to.

When you cast ptr1 you take its pointer _value_ and convert it
to a different type. As the new pointer type does not point to
a const qualified object, the compiler is no longer required
to issue a diagnostic.

This statement is legal because, in this case, the object being
pointed to (val) is not itself const qualified. However, this is
poor programming practice as it is simply an attempt by the
programmer to undermine the type safety that comes from using
const in the first place.
printf("\n%d %d",++val,*ptr1); //99 98 is output


Here you invoke undefined behaviour, so the code can do anything
at this point. It's equivalent to...

printf("\n%d %d", ++val, val);

....which is covered in the clc FAQ.

--
Peter

Nov 15 '05 #4

Peter Nilsson wrote:
ra*******@gmail.com wrote:
hello,
const int *ptr1 mean i can change value of ptr1 but not of
*ptr1 right?
Yes.
then why following snippet doesnot give error?
int val=50;
const int *ptr1=&val;
*(int *)ptr1=98; //what is this casting?


Because here you've overridden the nature of what ptr1 points to.

When you cast ptr1 you take its pointer _value_ and convert it
to a different type. As the new pointer type does not point to
a const qualified object, the compiler is no longer required
to issue a diagnostic.

Does you mean *(int *) creates new pointer type which has no name
but has storage in memory? I am confused about this cast type for const
types
*(int *)ptr1=98;
isn't this be same ptr1? i mean we just type cast and not even store
its result to any explicit variable?
This statement is legal because, in this case, the object being
pointed to (val) is not itself const qualified. However, this is
poor programming practice as it is simply an attempt by the
programmer to undermine the type safety that comes from using
const in the first place.
printf("\n%d %d",++val,*ptr1); //99 98 is output


Here you invoke undefined behaviour, so the code can do anything
at this point. It's equivalent to...

printf("\n%d %d", ++val, val);

...which is covered in the clc FAQ.

--
Peter


Nov 15 '05 #5
ra*******@gmail.com writes:
Peter Nilsson wrote:
ra*******@gmail.com wrote:
> hello,
> const int *ptr1 mean i can change value of ptr1 but not of
> *ptr1 right?


Yes.
> then why following snippet doesnot give error?
> int val=50;
> const int *ptr1=&val;
> *(int *)ptr1=98; //what is this casting?


Because here you've overridden the nature of what ptr1 points to.

When you cast ptr1 you take its pointer _value_ and convert it
to a different type. As the new pointer type does not point to
a const qualified object, the compiler is no longer required
to issue a diagnostic.

Does you mean *(int *) creates new pointer type which has no name
but has storage in memory? I am confused about this cast type for const
types
*(int *)ptr1=98;
isn't this be same ptr1? i mean we just type cast and not even store
its result to any explicit variable?


Terminology quibble: it's called a "cast", not a "type cast".

A cast is an operator that specifies a type conversion. If it appears
in an expression, it yields a value as its result; that value needn't
be stored anywhere in memory. In that sense, it's just like any other
operator. Given:
a = b * c + d;
the "*" operator specifies the result of multiplying b and c, but the
result of the multiplication needn't be stored. It's just part of the
expression.

So let's analyze "*(int *)ptr1=98" from the inside out.

ptr1 is the name of an object of type pointer-to-const-int. Evaluating
it in an expression yields a value of type pointer-to-const-int.

(int*)ptr1 converts that value from pointer-to-const-int to
pointer-to-int. This can be a dangerous thing to do if the int
being pointed to really is constant.

*(int*)ptr1 takes this pointer-to-const-int value and dereferences it,
yielding an lvalue of type int.

*(int*)ptr1 = 98 assigns the value 98 to the object pointed to by
ptr1. We couldn't have done this without the cast because ptr1 points
to a const int. The cast tells the compiler to pretend that the
object being pointed to isn't const. As it happens, the object being
pointed to is "val", which really isn't const, so this is ok.

Now, if the declaration of val were
const int val = 50;
then the compiler would be allowed (but not required) to store val in
read-only memory. The cast would tell the compiler not to complain
about the violation of val's constness, but it wouldn't necessarily
make it possible to change its value. The result would be undefined
behavior.

--
Keith Thompson (The_Other_Keith) 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.
Nov 15 '05 #6
On Thu, 08 Sep 2005 21:09:57 GMT, Keith Thompson <ks***@mib.org>
wrote:
ra*******@gmail.com writes:
Peter Nilsson wrote:
ra*******@gmail.com wrote:
> hello,
> const int *ptr1 mean i can change value of ptr1 but not of
> *ptr1 right?

Yes.

> then why following snippet doesnot give error?
> int val=50;
> const int *ptr1=&val;
> *(int *)ptr1=98; //what is this casting?

Because here you've overridden the nature of what ptr1 points to.

When you cast ptr1 you take its pointer _value_ and convert it
to a different type. As the new pointer type does not point to
a const qualified object, the compiler is no longer required
to issue a diagnostic.
Does you mean *(int *) creates new pointer type which has no name
but has storage in memory? I am confused about this cast type for const
types
*(int *)ptr1=98;
isn't this be same ptr1? i mean we just type cast and not even store
its result to any explicit variable?


Terminology quibble: it's called a "cast", not a "type cast".

A cast is an operator that specifies a type conversion. If it appears
in an expression, it yields a value as its result; that value needn't
be stored anywhere in memory. In that sense, it's just like any other
operator. Given:
a = b * c + d;
the "*" operator specifies the result of multiplying b and c, but the
result of the multiplication needn't be stored. It's just part of the
expression.

So let's analyze "*(int *)ptr1=98" from the inside out.

ptr1 is the name of an object of type pointer-to-const-int. Evaluating
it in an expression yields a value of type pointer-to-const-int.

(int*)ptr1 converts that value from pointer-to-const-int to
pointer-to-int. This can be a dangerous thing to do if the int
being pointed to really is constant.

*(int*)ptr1 takes this pointer-to-const-int value and dereferences it,
yielding an lvalue of type int.


Now it just a pointer to int, no const.

*(int*)ptr1 = 98 assigns the value 98 to the object pointed to by
ptr1. We couldn't have done this without the cast because ptr1 points
to a const int. The cast tells the compiler to pretend that the
object being pointed to isn't const. As it happens, the object being
pointed to is "val", which really isn't const, so this is ok.

Now, if the declaration of val were
const int val = 50;
then the compiler would be allowed (but not required) to store val in
read-only memory. The cast would tell the compiler not to complain
about the violation of val's constness, but it wouldn't necessarily
make it possible to change its value. The result would be undefined
behavior.

<<Remove the del for email>>
Nov 15 '05 #7
Barry Schwarz <sc******@deloz.net> writes:
On Thu, 08 Sep 2005 21:09:57 GMT, Keith Thompson <ks***@mib.org>
wrote:

[...]
*(int*)ptr1 takes this pointer-to-const-int value and dereferences it,
yielding an lvalue of type int.


Now it just a pointer to int, no const.


Quite right, thanks for the correction.

--
Keith Thompson (The_Other_Keith) 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.
Nov 15 '05 #8

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

Similar topics

3
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
6
by: AlesD | last post by:
Hello, I can't figure out how to build assignment operator for class which contains "type* const value". --- example --- class parent_t; class child_t {
14
by: Joost Ronkes Agerbeek | last post by:
Should I remove const from a private member just for the sake of the assignment operator? I have a class that looks something like this. class Foo { public: Foo(const std::string&...
9
by: vashwath | last post by:
Hi all, Recently I attended an interview in which the question "Is there any difference between "const T var" and "T const var"? was asked.I answered "NO"(I guessed it:-( ).Did I answered...
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
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,...
11
by: Christopher Key | last post by:
Hello, Can anyone suggest why gcc (-W -Wall) complains, test.c:22: warning: passing arg 1 of `f' from incompatible pointer type when compiling the following code? Change the declation of f...
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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: 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
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...
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
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...

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.