Solved

# Constant Pointers and Pointers to Constants

Posted on 2009-04-27
387 Views
Hi,

I want to know the exact difference between "Constant Pointers and Pointers to Constants"
I want to know it in detail.

Please give some example to understand and practice.

Thanks and Regards,

0

LVL 19

Expert Comment

0

LVL 39

Expert Comment

Constant Pointers: The pointer itself is constant and cannot be changed to point to anything else but the object being pointer to can be modified

Pointers to Constants: The object being pointer to is constant and cannot be modified, but the pointer can

You can also have Constant Pointers pointing to Constants, which means neither the pointer nor the object it points to can be modified.
0

Author Comment

some example please to better understand
0

LVL 19

Expert Comment

// how to declare a pointer to a constant character
const char * myPtr = &char_A;

// how to declare a constant pointer to a character.
char * const myPtr = &char_A;

// how to declare a constant pointer to a constant character.
const char * const myPtr = &char_A;

0

LVL 39

Assisted Solution

Constant Pointers:
char * const

Pointers to Constants:
char const *

or

const char *

Constant Pointers pointing to Constants:
char const * const

or

const char * const
0

LVL 39

Accepted Solution

>>>> some example please to better understand

(1) pointer to const data

void func(const char * psz)
{
strcpy(psz, "Hello World");   // error

// the strcpy copies the text of the 2nd argument to the address the
// first argument is pointing to. That is not allowed as the pointer points
// to const data

psz = "Hello World";   // o. k.

// That is good cause the data the psz was pointing to did not change.
// What changes is the value of the pointer variable psz which before
// the assignment  pointed to some text the caller had provided and
// now points to some memory where the literal "Hello World" was stored

const char sznew[10] = "Hello all";  // ok

// that is ok although the array data were const. Here we have not
// an assignment but an initialization (construction) where even
// const data can get an (initial) value.

}

(2) const pointer

void f (char * pcsz const, int siz)
{
strncpy(pcsz, "Hello World", min(12, siz));   // ok

// the strcpy copies the text of the 2nd argument to the address the
// first argument is pointing to. That is allowed here as the pointer
// variable pcsz which is const wasn't changed by the operation
// of initially copying data to the array constructed

pcsz = "Hello World";   // doesn't compile

// That is bad cause the the pcsz may not be changed by assigning
//  another address to the pointer

char * psznew const = new char[20]; // ok
strcpy(psznew, "Hello World");  // ok

// that is both ok although the pointer was const. Here we have not
// an assignment but an initialization (construction) of the pointer
// and the strcpy doesn't change the const pointer but only the data
// the pointer is pointing to

}

0

## Featured Post

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.