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;
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:
Post a Comment