-
November 25th, 2013, 11:03 AM
#1
[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)
-
November 25th, 2013, 11:12 AM
#2
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.
-
November 25th, 2013, 11:19 AM
#3
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?
-
November 25th, 2013, 11:40 AM
#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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 25th, 2013, 11:43 AM
#5
Re: about void pointers
Originally Posted by 2kaud
Of what type is c?
sorry i'm speaking about void pointers
void *c;
-
November 25th, 2013, 12:02 PM
#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
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 25th, 2013, 12:12 PM
#7
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
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?
-
November 25th, 2013, 12:32 PM
#8
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.
-
November 25th, 2013, 12:33 PM
#9
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?
-
November 25th, 2013, 12:34 PM
#10
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
-
November 25th, 2013, 12:36 PM
#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
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 25th, 2013, 12:36 PM
#12
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"
-
November 25th, 2013, 12:40 PM
#13
Re: about void pointers
thanks
but for next array index?
-
November 25th, 2013, 12:41 PM
#14
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
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.
-
November 25th, 2013, 12:42 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|