Link to home
Start Free TrialLog in
Avatar of palhade
palhadeFlag for India

asked on

meaning of const void * const pMem

Hi,

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

thanks
Avatar of Infinity08
Infinity08
Flag of Belgium image

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.


So,

int n = 0;

const int * const p = &n;

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


>> 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.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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:

(A)
It defines a pointer variable pointing to a void type

(B)
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"
http://duramecho.com/ComputerInformation/WhyHowCppConst.html
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.