Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 12th, 2007, 03:37 AM
Authorized User
 
Join Date: Aug 2006
Location: Mumbai, Maharashtra, India.
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
Location: , , .
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
Location: , , .
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
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:47 PM.


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