CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Senior Member
Join Date
Apr 2009
Posts
1,228

## [RESOLVED] about void pointers

can anyone explain to me how can i use the void pointers?
i understand that can recive an adress variable(&). but can recive a value?

Code:
int a=5;
void *d;
b=&a;
b=100;//??? why i can't do these?(ok i don't remember everything about pointers, but please tell me these)

2. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: about void pointers

You didn't show the definition of b, but
void* b = (void*)100 is legal, although probably not a good idea.

3. Senior Member
Join Date
Apr 2009
Posts
1,228

## Re: about void pointers

Originally Posted by GCDEF
You didn't show the definition of b, but
void* b = (void*)100 is legal, although probably not a good idea.
thanks
i used: cout << "\n" << c;
but i get a strange result. seems be in hexadecimal... why?

4. ## Re: about void pointers

Originally Posted by Cambalinho
i used: cout << "\n" << c;
but i get a strange result. seems be in hexadecimal... why?
Of what type is c?

5. Senior Member
Join Date
Apr 2009
Posts
1,228

## Re: about void pointers

Originally Posted by 2kaud
Of what type is c?
sorry i'm speaking about void pointers
void *c;

6. ## Re: about void pointers

In this example

Code:
#include <iostream>
using namespace std;

int main()
{
void* c = (void*)94;

cout << c << endl;
cout << (int)c << endl;
return 0;
}
gives the output
Code:
0000005E
94
c is a pointer to void. By default, cout displays pointers in hex. To get the output in decimal, cast the variable to an int or some other type that cout displays in decimal.

7. Senior Member
Join Date
Apr 2009
Posts
1,228

## Re: about void pointers

Originally Posted by 2kaud
In this example

Code:
#include <iostream>
using namespace std;

int main()
{
void* c = (void*)94;

cout << c << endl;
cout << (int)c << endl;
return 0;
}
gives the output
Code:
0000005E
94
c is a pointer to void. By default, cout displays pointers in hex. To get the output in decimal, cast the variable to an int or some other type that cout displays in decimal.
thanks
see my new exemple:
Code:
std::vector<int> b;
b.resize(3);
b[0]=100;
b[1]=200;
b[2]=300;
c=(void *)"hello world";
cout << "\n" <<(char *) c;
c=&b[0];
cout << "\n" <<*c;
sorry what i'm doing wrong?

8. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: about void pointers

Originally Posted by Cambalinho
thanks
see my new exemple:
Code:
std::vector<int> b;
b.resize(3);
b[0]=100;
b[1]=200;
b[2]=300;
c=(void *)"hello world";
cout << "\n" <<(char *) c;
c=&b[0];
cout << "\n" <<*c;
sorry what i'm doing wrong?
If you're trying to get c to = 100, you should set it to b[0], not &b[0];

What you're doing doesn't make a whole lot of sense though. Every post you've made so far has used variables where you don't show the declaration. Properly worded questions will get you much better answers.

9. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,748

## Re: about void pointers

Your compiler should be giving you an error message telling you what you are doing wrong. What is the error message and what do you think it means?

10. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: about void pointers

Originally Posted by Cambalinho
thanks
see my new exemple:
If "c" is a void pointer, how is the compiler supposed to know what to do with it when you ask for it to be dereferenced?
Code:
c=&b[0];
cout << "\n" << *c;
A void pointer cannot be dereferenced unless you tell the compiler (by casting) what that void pointer is supposed to be pointing to.

Regards,

Paul McKenzie

11. ## Re: about void pointers

Code:
#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> b;

b.resize(3);
b[0]=100;
b[1]=200;
b[2]=300;

void * c=(void *)"hello world";

cout <<(char *) c << endl;
c = &b[0];
cout << *(int*)c;
return 0;
}
Produces the output
Code:
hello world
100
c is a pointer to void. To dereference the pointer and display its contents, cout needs to know to what type of data c is pointing. You used a cast for the first cout statement and you need another one for the second.

12. Senior Member
Join Date
Apr 2009
Posts
1,228

## Re: about void pointers

Originally Posted by Paul McKenzie
If "c" is a void pointer, how is the compiler supposed to know what to do with it when you ask for it to be dereferenced?
Code:
c=&b[0];
cout << "\n" << *c;
A void pointer cannot be dereferenced unless you tell the compiler (by casting) what that void pointer is supposed to be pointing to.

Regards,

Paul McKenzie
c=& b[0];
cout << "\n" << *c;

error message:
"error: 'void*' is not a pointer-to-object type"

13. Senior Member
Join Date
Apr 2009
Posts
1,228

## Re: about void pointers

thanks
but for next array index?

14. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: about void pointers

Originally Posted by 2kaud
Code:
#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> b;

b.resize(3);
b[0]=100;
b[1]=200;
b[2]=300;

void * c=(void *)"hello world";

cout <<(char *) c << endl;
c = &b[0];
cout << *(int*)c;
return 0;
}
Produces the output
Code:
hello world
100
c is a pointer to void. To dereference the pointer and display its contents, cout needs to know to what type of data c is pointing. You used a cast for the first cout statement and you need another one for the second.
You're also dereferncing the pointer to get cout to produce 100. That may or may not be what the op is trying to do. Hard to tell. He's trying to change the value of the pointer, or modify the string literal that it points to. Either way, it's a bad idea.

15. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: about void pointers

Originally Posted by Cambalinho
thanks
but for next array index?
Using words, not code, what are you trying to do?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.