|
14 Jan 2003, 16:14
|
#1
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Pointer problem...
I'm trying to pass a void pointer with a function so I can use sprintf to put it in a string. Problem is that I cannot get the value behind the pointer:
Code:
void Game3D::setParam(char* filename, char* preset, void* value, char type)
{
char ln[100];
char newline[100];
sprintf(ln,"%s %%%c",preset,type); //result: NEWLINE = <preset> %<type>
sprintf(newline,ln,*value); //result: NEWLINE = NEWLINE, with %<type> replaced
MSGBOX(newline);
The second sprintf causes the problem, where I try to get the value behind the value pointer. This syntax gives an "illegal indirection" error. If I leave the '*' out I get the address of the pointer.
Anyone know how I can make this work? The value behind the pointer can be a bool, float or int.
|
|
|
14 Jan 2003, 16:43
|
#2
|
Tourist
Join Date: Jun 2001
Location: moon
Posts: 90
|
I think, u have to cast value depending on type:
like:
if(type == "f") sprintf(newline,ln,*((double*)value));
else if(type == ...
__________________
Quote:
Originally posted by Bloomers III
sex is dirty and for losers who can't masturbate properly
|
|
|
|
15 Jan 2003, 10:15
|
#3
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
Re: Pointer problem...
Quote:
Originally posted by Structural Integrity
I'm trying to pass a void pointer with a function so I can use sprintf to put it in a string. Problem is that I cannot get the value behind the pointer:
Code:
void Game3D::setParam(char* filename, char* preset, void* value, char type)
{
char ln[100];
char newline[100];
sprintf(ln,"%s %%%c",preset,type); //result: NEWLINE = <preset> %<type>
sprintf(newline,ln,*value); //result: NEWLINE = NEWLINE, with %<type> replaced
MSGBOX(newline);
The second sprintf causes the problem, where I try to get the value behind the value pointer. This syntax gives an "illegal indirection" error. If I leave the '*' out I get the address of the pointer.
Anyone know how I can make this work? The value behind the pointer can be a bool, float or int.
|
Can you not just create 3 overloaded functions changing void* value to bool*, float*, int* and then hope it picks up the right one?
|
|
|
15 Jan 2003, 16:30
|
#4
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
I don't think overloading would be wise... my Game3D file is cluttered enough as it is.
Instead, I made a switch for the int and float and two different sprintf's and used the casting idea of Pitchfork.
The only problem I have now is a boolean. I used to pass them as an unsigned int and hoped that they are converted properly in the read variant of this function, but it appears this doesn't work in this case. It make a 0xffff of the bool, regardless of the value. Any ideas for this? Is there a conversion string ("%b" or so) for a boolean? My book doesn't list it.
|
|
|
15 Jan 2003, 16:39
|
#5
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
Another switch and a temp var storing an int value of 0 or 1? Then %d that...
|
|
|
15 Jan 2003, 17:15
|
#6
|
Tourist
Join Date: Jun 2001
Location: moon
Posts: 90
|
bool is defined via:
typedef int BOOL;
so at a quick look you should simply treat them as int
__________________
Quote:
Originally posted by Bloomers III
sex is dirty and for losers who can't masturbate properly
|
|
|
|
15 Jan 2003, 17:29
|
#7
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
sprintf("%s", *(BOOL *)value ? "верно" : "Не верно")?
|
|
|
15 Jan 2003, 18:58
|
#8
|
∞+♪˛
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
|
Quote:
Originally posted by queball
sprintf("%s", *(BOOL *)value ? "верно" : "Не верно")?
|
I've actually been wondering why MSVC doesn't seem to compile unicode .cpp files.
Think it's silly not being able to use variable names such as θ and Δx.
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics... 猫
<!--Last incarnation: Nov 2000-->
|
|
|
|
All times are GMT +1. The time now is 06:10.
| |