CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2005
    Posts
    200

    What is the difference between these 2 const pointers?

    For these 2 types of const pointers, what is the difference between them?

    const int * pData2; and

    int * const pData3;

    ?

  2. #2
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: What is the difference between these 2 const pointers?

    In the first, the int (i.e. the data being pointed to) is const. In the second, the pointer itself is const.
    - Alon

  3. #3
    Join Date
    Oct 2004
    Posts
    296

    Re: What is the difference between these 2 const pointers?

    To understand pointers, you have to understand how data is stored in a computer's memory. You can think of memory as millions of boxes that you can put things into. Here is a diagram:
    Code:
    memory:                 box1           
                         +---------+
                         |         |
                         |         |
                         |         |
                         +---------+
    
                            box2
                         +---------+
                         |         |
                         |         |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                         |         |
                         |         |
                         |         |
                         +---------+
    If you create an int variable and assign it a number, then the number gets stored in memory somewhere, i.e. the number gets stored in one of the boxes:
    Code:
    int num = 10;
    
    memory:                 box1           
                         +---------+
                         |         |
                         |         |
                         |         |
                         +---------+
    
                            box2
                         +---------+
                         |  num=   |
                         |   10    |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                         |         |
                         |         |
                         |         |
                         +---------+
    If you create a pointer to the number, then this happens:
    Code:
    int num = 10;
    int* pint = #
    
    memory:                 box1           
                         +---------+
                         |         |
                         |         |
                         |         |
                         +---------+
    
                            box2
                         +---------+
                         |  num=   |
    pint ------------->  |   10    |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                         |         |
                         |         |
                         |         |
                         +---------+
    You can make pint point to a different box by doing this:
    Code:
    int age = 7;
    pint = &age;
    
    
    memory:                 box1           
                         +---------+
                         |         |
                         |         |
                         |         |
                         +---------+
    
                            box2
                         +---------+
                         |  num=   |
                         |   10    |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                         |  age=   |
    pint --------------> |   7     |
                         |         |
                         +---------+
    You can also use pint to change the value in a box:
    Code:
    *pint = 40;
    
    memory:                 box1           
                         +---------+
                         |         |
                         |         |
                         |         |
                         +---------+
    
                            box2
                         +---------+
                         |  num=   |
                         |   10    |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                         |  age=   |
    pint --------------> |   40    |
                         |         |
                         +---------+
    By using the const modifier, you can prevent the pointer from changing the value in a box:
    Code:
    int size = 2;
    const int* pint = &size;
    *pint = 30;  //ERROR
    
    memory:                 box1           
                         +---------+
                   const | size=   |
    pint ------------->  |   2     |
                         |         |
                         +---------+
    
                            box2
                         +---------+
                         |  num=   |
                         |   10    |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                         |  age=   |
    pint --------------> |   40    |
                         |         |
                         +---------+
    However, the pointer can still be made to point to another box:
    Code:
    pint = &age;
    
    memory:                 box1           
                         +---------+
                         | size=   |
                         |   2     |
                         |         |
                         +---------+
    
                            box2
                         +---------+
                         |  num=   |
                         |   10    |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                   const |  age=   |
    pint --------------> |   40    |
                         |         |
                         +---------+
    Or, you can use the const modifier to prevent pint from pointing to another box:

    Code:
    int* const pint = #
    pint = &age;  //ERROR
    
    memory:                 box1           
                         +---------+
                         | size=   |
                         |   2     |
                         |         |
                         +---------+
    
                            box2
                         +---------+
         const           |  num=   |
    pint --------------> |   10    |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                         |  age=   |
                         |   40    |
                         |         |
                         +---------+
    Even though the pointer cannot be made to point to another box, you can still use the pointer to change what's in the box:
    Code:
    *pint = 35;
    
    memory:                 box1           
                         +---------+
                         | size=   |
                         |   2     |
                         |         |
                         +---------+
    
                            box2
                         +---------+
         const           |  num=   |
    pint --------------> |   35    |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                         |  age=   |
                         |   40    |
                         |         |
                         +---------+
    You can also use the const modifier twice:
    Code:
    const int* const pint = &size;
    
    
    memory:                 box1           
                         +---------+
                         | size=   |
                         |   2     |
                         |         |
                         +---------+
    
                            box2
                         +---------+
         const     const |  num=   |
    pint --------------> |   35    |
                         |         |
                         +---------+
    
    
                            box3
                         +---------+
                         |  age=   |
                         |   40    |
                         |         |
                         +---------+
    That means that the pointer cannot be moved to point to another box, AND the value in the box cannot be changed:
    Code:
    pint = &size;  //ERROR
    *pint = 100;   //ERROR
    How do you pronounce these pointer names? When you write this:
    Code:
    const int* pint = &size;
    you say, "pint points to a const int". In in other words, you read it from right to left with the * dividing the two parts to be read: "pint" is a pointer to a "const int". And when you write this:
    Code:
    int* const pint = &size;
    you say, "pint is a const pointer to an int". And when you write this:
    Code:
    const int* const pint = &size;
    you say that "pint is a const pointer to a const int".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured