meaning of const void * const pMem

Posted on 2009-04-21
Last Modified: 2012-06-27

I want to understand menaing of "meaning of const void * const pMem"

Question by:palhade
    LVL 53

    Expert Comment

    The trick is to read from right to left :

            "pMem is a const pointer (*) to a const void"

    So, it's a const pointer, meaning that the pointer itself cannot be modified.
    And, it's pointing to const data, meaning that we cannot modify that data by using this pointer.
    LVL 10

    Expert Comment

    pMem is a constant pointer to a constant void

    However, it might be best to take the void out, and consider something like this instead

    const int * const p

    Here, p is a constant pointer to a constant int - meaning that you cannot [should not ;-) ] alter the pointer's value, OR the value of the int pointed to.


    int n = 0;

    const int * const p = &n;

    You cannot do, for example, p++, or *p = 1;

    LVL 39

    Expert Comment

    >> const void * const pMem
    It is a constant pointer to a constant type of void. The void pointer is a generic pointer type that can be set to point to any object. It is often used in C to allow the address of unknown objects to be passed to a function. For example, think of memcpy that uses this pointer type. In C++ it has very little use because (a) C++ focuses on things being type safe and (b) C++ has templates.
    LVL 53

    Accepted Solution

    Or, in code :
    int value = 5;
    int value2 = 10;
    /* const pointer-to-const : */
    const int * const p1 = &value;
    p1 = &value2;                    /* ERROR : cannot modify p1, since it's a const pointer */
    *p1 = 20;                        /* ERROR : cannot modify *p1, since p1 is a pointer-to-const */
    /* pointer-to-const : */
    const int *p2 = &value;
    p2 = &value2;                    /* OK */
    *p2 = 20;                        /* ERROR : cannot modify *p2, since p2 is a pointer-to-const */
    /* const pointer : */
    int * const p3 = &value;
    p3 = &value2;                    /* ERROR : cannot modify p3, since it's a const pointer */
    *p3 = 20;                        /* OK */
    /* pointer : */
    int * p4 = &value;
    p4 = &value2;                    /* OK */
    *p4 = 20;                        /* OK */

    Open in new window

    LVL 39

    Expert Comment

    Let me try also an attempt (not cause any of the above comments was wrong or incomplete but to maybe set a different focus).

    Let's first remove the const keywords (they have been explained excellently above). Then we have

      void * pMem;

    That can be read in two ways:

    It defines a pointer variable pointing to a void type

    It defines a variable of  type void pointer.

    The (A) often was used by C programmers (though not in the explanations above) and also by the C or C++ compiler. Here a pointer is just another way to access the 'real' data and there are little to no differences in the semantics using a pointer or a non-pointer. You often can recognize the (A) 'view' when the asterisk is placed directly at the variable and not at the type:

      void *pMem;

    The (B) 'view' is favorite of most C++ programmers, but not only. If someone writes

       void* pMem;  

    you can be sure that it is a (B) programmer ;-)

    If you see  

       void * pMem;

    (what is my current project style) it is indifferent (but probably a B).

    But even in pure C you might find a statement like

    typedef void *  VOIDPTR;

    what is a obvious commitment to (B).

    LVL 19

    Expert Comment

    Here you can reade more about const declaration:
    "The C++ 'const' Declaration: Why & How"
    LVL 53

    Expert Comment

    May I ask why you gave a B grade ? That usually means that something was missing in the answer and/or that something is still unclear. If that's the case, then please do not hesitate to ask for clarification where needed.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    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.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now