I have been experimenting with void pointers. I have encountered many problems, since I want to implement them into a very big project, but I made some examples that I believe that if I understand them, I will be able to use them efficiently.
Here is my first example:
int main (int argc, char * const argv[]) {
int *p;
int a = 100;
void *v;
p = &a;
v = p;
std::cout << v;
return 0;
}
The result I am getting is '0xbffff9c0' from the cout function. Actually, I had predicted it, but I don't know how can I access the contents of the void pointer. Can you help?
Another example:
void f(void *p){
printf("%s",p);
}
int main (int argc, char * const argv[]) {
char p[] = "hello!";
f(p);
return 0;
}
The above code works as expected. It shows 'hello'. But if I replace the printf fuunction with the cout:
void f(void *p){
cout << p;
}
int main (int argc, char * const argv[]) {
char p[] = "hello!";
f(p);
return 0;
}
I get an address! (0xbffff9b8).
Can you please explain to me why this is happenning (in both examples) and what can I do to dereference a void pointer so that I can access the elements it points to?