meaning of const void * const pMem


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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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;

evilrixSenior Software Engineer (Avast)Commented:
>> 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.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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).

Here you can reade more about const declaration:
"The C++ 'const' Declaration: Why & How"
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.