You are here:

C++/C++ Void Pointer


Hello Eddie, Thanks a lot for your answer. I tried the Line 5 as  "(int*)ptrV = &i1" as mentioned by you.

But the M/S Visual Studio compiler (Visual C++ 6.0) is giving an error - "error C2106: left operand must be l-value".

I tried the following code:

int i1=5;
void *ptrV1;
ptrV1 = &i1;  // This line works, but may be unpredictable.
(int*) ptrV1 = &i1;  // This line gives the above error C2106.
(int*) (ptrV2 = &i1);  // This line works fine.

cout << "Void Pointer: " << *(int*)ptrV1;   // This works fine.

I found out that the above line works when I put parenthesis all around the assignment statement. Kindly let me know your opinion.
Thanks a lot - Sukhendu

Followup To Question -
Hi Eddie, I have a question on C++ void pointer assignment. Lets take this Code snippet:

 Line 1 >> int i1=5 ;
 Line 2 >> int *ptrI ;
 Line 3 >> void *ptrV ;
 Line 4 >> ptrI = &i1;
 Line 5 >> ptrV = (int *) &i1;

While assigning the address of integer variable to void pointer (in Line 5) the explicit type cast is required. Whereas in line 4, this explicit type cast is not required. My idea is that the resultant of the right hand side on Line 4 (address of integer variable) is a pointer to an integer. Therefore we don't explicitly type cast in Line 4. So for Line 5, when the address to integer variable is a pointer to an integer, what is the need to explicitly type cast again unless we want to assign different pointer other than integer pointer. The compiler should be intelligent enough to assign an integer pointer to the void pointer in Line 5 without explicit type cast.
Could you please give me a logical explanation on this type cast in Line 5.

Thanks a lot - Sukhendu

Answer -
Hello Sukehndu Sain, thank you for the question.

The way you have line 5 written should cause a warning or error on most compilers. The appropriate way to cast on line 5 is like this:

(int*)ptrV = &i1;


ptrV = (void*)&i1;

Void pointers are tricky. They come in handy sometimes, but are not type safe and will crash your application if you miscast one anywhere. That is why the creaters of C++ came up with templates as the superior alternative.

The reason you have to explicitly cast on line 5 is because that a pointer to void could be anything at all, and thus has an unknown size. To help understand that better, think about what would happen if you would do the following without an explicit cast:

std::cout << "The value of the void pointer is: " << (*ptrV);

The compiler wouldn't know if the pointer's value is an int, char, boolean, what have you. What if the value of the void pointer is 0, which was assigned as an integer. It's now printing a void pointer, and 0 could very well be false. In fact, if you cast it to a boolean when you print it, it would print false(or 0, depending on the compiler). Without a cast on that line, you are going to crash. That is why you need an explicit cast on line 5.

I hope this information was helpful.

- Eddie

Hello Sukehndu Sain, thank you for the question.

This appears to be a compiler specific error. I believe that using the statement like this:

((int*)ptrV) = &i1;

would also disperse that error, as it is more than likely just Visual Studio being picky. It could also be an evaluation problem where it was having problems trying to decide wether or not to cast or assign first. I'm not too sure actually, I use Visual Studio 7.1. Sorry about that!

I hope this information was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2017 All rights reserved.

[an error occurred while processing this directive]