Simply way to understand is to read from left to right:
1) const char * a --> Pointer to character constant. Can point to another constant but value of constant cannot be changed using pointer.
2) char * const a --> Constant pointer to character. Cannot be pointed to another address but can change value pointed by pointer.
Type Cast Pointer constant and then change the value.
Posted by DUS
on 04/05/2013 10:26pm
int i = 9;
const int *p = &i;
*p = 30; // This would result in ERROR
*((int *)p) = 40; // This allows me to change the Value of i. Why?
Does the type cast create its own variable and then allow to change the value of i? And another case is if declaration of i is changed to cont int i = 9; then the code gets compiled and in run time the value of i is not changed. Can you explain me this. please Thanks
C style cast
Posted by Alan
on 04/15/2013 04:14am
The C style cast removes the const modifier therefore allowing you to change the variable. In this case
*((int*)p) = 40;
is equivalent to
const_cast(p) = 40;
Using C++ style casts removes ambiguity and is to be preferred (unless you're actually writing C of course!)
Posted by Alan
on 04/15/2013 07:02am
Except that the bit of the const_cast statement in angle brackets has been stripped out. There doesn't seem to be any help, nor a preview facility, so all I can do is hope that this will display correctly:
*((int*)p) = 40; is equivalent to const_cast(p) = 40;
thanks for sharing! I have a proplem to report when programing.so look code first:
const int test = 5;
const int *pm = &test;
int *pn = pm;
Than I use gcc(version:4.6.1) compiler to make it.But there is no error happened,and output the result:5,6,6.so I want to ask for your help. the code use a const pointer to modify a const.so why???
I want to use this to optimize my program.
Let's say I have a structure like this that I use for many stuff.
unsigned char workspace[1024*1024];
unsigned int flags;
I have this declared in my_stuff.c :
and in my_stuff.h:
extern my_stuff_t my_stuff;
What I want to do is to have another data type points into this structure, in another file.
For example, in my_data.c:
custom_t* const data =(custom_t *) (my_stuff.workspace[2*1024]);
and in my_data.h:
extern custom_t* const data;
What I want is that the compiler directly compute the value of data when dealing with it in external code. But when I generate the assembly code, it seems that gcc is still using a load instruction to read "data" address from the pointer in memory. Off course, any code in my_data.c that use "data" is fine (assembly code is as expected, with direct use of my_stuff address), I'm talking about external object files that include my_data.h
So, is that possible to force the compiler to assume the address is contant and thus not reading it from the pointer address but use a precomputed symbol (later resolved by linker) ?
What is the diff between
int const *ptr
const int *ptr
Determined by the Position of the Dereference Operator
Posted by andy
on 07/21/2012 08:32pm
When 'const' comes before the '*', the thing being declared constant is *ptr, i.e. the int being pointed to by ptr. When it comes after the '*', it is the pointer itself that is being declared constant.