• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

Constant Pointers and Pointers to Constants

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
palhade
Asked:
palhade
2 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
palhadeAuthor Commented:
some example please to better understand
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
alb66Commented:
// 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
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
itsmeandnobodyelseCommented:
>>>> 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now