Void Pointers

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main ()
{
 	void *a ; 
        int b=4;
	a=&b;
}

now how can i access the value of the pointer a ?
The value of a is the memory address of b.

Use the asterisk (*) dereference operator to modify the value at the address contained in the pointer.
The * has a double role as it is used for declaring pointers as well.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main ()
{
 	void *a ; 
        int b=4;
	a=&b;

	*static_cast<int *> (a) = 55; // here * used as dereference operator

	cout << b;
}


In your special case you'll have to cast the pointer.
Casting by static_cast<int *> (a) is a way of hinting to the compiler what type to consider the data, as int.
Because using void for pointers means "I don't care".
Topic archived. No new replies allowed.