• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1355
  • Last Modified:

meaning of const void * const pMem

Hi,

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

thanks
0
palhade
Asked:
palhade
1 Solution
 
Infinity08Commented:
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.
0
 
peetmCommented:
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;


0
 
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Infinity08Commented:
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

0
 
itsmeandnobodyelseCommented:
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).





0
 
alb66Commented:
Here you can reade more about const declaration:
"The C++ 'const' Declaration: Why & How"
http://duramecho.com/ComputerInformation/WhyHowCppConst.html
0
 
Infinity08Commented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now