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.