const int * assignment

R

rahul8143

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
 
A

Alexei A. Frounze

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
 
M

Marc Thrun

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).
 
P

Peter Nilsson

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.
 
R

rahul8143

Peter said:
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?
 
K

Keith Thompson

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.
 
B

Barry Schwarz

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>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top