Bharat Banate's Work Profile

View Bharat Banate's profile on LinkedIn

Thursday, June 19, 2008

C++: void pointers

The keyword 'void' can be used to define a pointer to a generic term. In C++, special care has to be taken to handle the assignment of void pointers to other pointer types. Following code shows the same:

void *p;
char *s;
p = s;
s = p;

Here, the second assignment would flag an error indicating a type mismatch. While you can assign a pointer of any type to a void pointer, the reverse is not true unless you specifically typecast it as shown below:

s = (char*) p;


0 comments: