Constant Pointers and Pointers to Constants

In the CodeGuru newsletter, I brought up the topic of constant pointers and pointers to constants. While this is a beginning level topic, it is one that some advanced-level people goof up in their code.

Pointer contants and contant pointers are also something that many people simply don't use. If you have a value in your program and it should not change, or if you have a pointer and you don't want it to be pointed to a different value, you should make it a constant with the const keyword.

There are generally two places that the const keyword can be used when declaring a pointer. Consider the following declaration:

char A_char = 'A';
char * myPtr = &A_char;

This is a simple declaration of the variable myPtr. myPtr is a pointer to a character variable and in this case points to the character 'A'.

Don't be confused about the fact that a character pointer is being used to point to a single character—this is perfectly legal! Not every character pointer has to point to a string.

Now consider the following three declarations assuming that char_A has been defined as a type char variable.:

Comments

Programmer

Posted by C Coder
on 10/02/2014 10:42am

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

oops!

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;

I give up

hi

Posted by wubo
on 11/09/2012 12:07am

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;
printf("%d\n",(*pn)++);
printf("%d\n",*pm);
printf("%d\n",test);
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???

hi

Posted by bowu
on 11/09/2012 12:04am

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;
printf("%d\n",(*pn)++);
printf("%d\n",*pm);
printf("%d\n",test);
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???

a problem

Posted by wubo
on 11/09/2012 12:03am

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;
printf("%d\n",(*pn)++);
printf("%d\n",*pm);
printf("%d\n",test);
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???

citation needed

Thanks....

code optimization using const

Posted by cscos
on 10/26/2011 11:08am

Hello,
I want to use this to optimize my program.
Let's say I have a structure like this that I use for many stuff.
typedef struct
{
unsigned char workspace[1024*1024];
unsigned int flags;
} my_stuff_t;
I have this declared in my_stuff.c :
my_stuff_t my_stuff;
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:
#include "mystuff.h"
typedef struct
{
...
} custom_t;
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) ?

Top White Papers and Webcasts

Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT
The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility.
Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

On-demand Event
Event Date: February 12, 2015
The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …