Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 12th, 2007, 03:37 AM
Authorized User
 
Join Date: Aug 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to amit_mande@yahoo.com Send a message via AIM to amit_mande@yahoo.com Send a message via MSN to amit_mande@yahoo.com Send a message via Yahoo to amit_mande@yahoo.com
Default Constant pointer and pointer to a constant

Expalin me about following statements

char * const ptr1="GOOOD";//constant pointer

int const * ptr2=&m; //pointer to a constant

What is difference between them?
I can't get these statements.
Please help me.

Thank you in advanced.

Reply With Quote
  #2 (permalink)  
Old May 17th, 2007, 02:18 AM
Registered User
 
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi friend,

Look at this below code snippet, it sure help you in understanding the differences :)

void Foo( int * ptr,
          int const * ptrToConst,
          int * const constPtr,
          int const * const constPtrToConst )
{
    *ptr = 0; // OK: modifies the pointee
    ptr = 0; // OK: modifies the pointer

    *ptrToConst = 0; // Error! Cannot modify the pointee
    ptrToConst = 0; // OK: modifies the pointer

    *constPtr = 0; // OK: modifies the pointee
    constPtr = 0; // Error! Cannot modify the pointer

    *constPtrToConst = 0; // Error! Cannot modify the pointee
    constPtrToConst = 0; // Error! Cannot modify the pointer
}

Regards,
S.Manivannan


Reply With Quote
  #3 (permalink)  
Old June 5th, 2007, 01:39 AM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Constant Pointer: It means that the address that the pointer is pointing to cannnot be changed. Consider this
char str[] = "Hello";
char * const ptr = &str;
In the above two statements first we have created an array of char called str then in the next statement we have created a char pointer ptr which points to str. Since prt is a constant pointer it can only point to str. i.e if we write something like ptr = &str1. the compiler will flag an error because we cannot change the contents of pointer ptr though we can change the contents of string str;

Pointer to Constant: It simply means that the string is constant and the pointer is not constant. Now we can change the contents of pointer but not that of string through the use of pointer.
char str[] = "Hello";
const char * ptr = &str;
char str1[] = "World"
*(ptr+2) = 't' //error constant pointer cannot be used to modify string
ptr = &str1 //ok

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to struct goscho C++ Programming 1 January 10th, 2008 07:23 AM
Pointer fReqZz C++ Programming 5 June 5th, 2007 01:09 AM
Pointer reference perstam C++ Programming 5 April 29th, 2006 08:51 AM
NULL Pointer kumar_raj13 C++ Programming 0 July 15th, 2005 01:10 AM
pointer-to-function in C++ jacob C++ Programming 2 October 23rd, 2004 05:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.