Skip to main content
GameDev.net gamedev.net
🔒 Locked

void pointer a sprintf

Started by Structural Jan 14, 2003 at 9:54 AM 5 replies 5.3k views
Original Post
Structural
Structural
I''m trying to pass an arbitrary variable with a void pointer to a sprintf function: setParam(char* filename, char* preset, void* value, char type) { char ln[100]; char newline[100]; sprintf(ln,"%s %%%c",preset,type); //result: NEWLINE = % sprintf(newline,ln,*value); //result: NEWLINE = NEWLINE, with % replaced However, on the second sprintf it gives a compiler error: "illegal indirection". Is it possible to get the value behind the pointer? How would I do that? TY
STOP THE PLANET!! I WANT TO GET OFF!!
Paradigm Shifter
Paradigm Shifter
That's quite hard to understand what you're trying to do there... I'll have a go though.

Are you trying to do someting like

setParam( /* first param ignored */, "NEWLINE", pAnInteger, 'd' );

to get sprintf argument to expand to "%d"?

You'll need to cast the void* as the correct type (you can't do * or ++ on a void*, that depends on the sizeof which is unknown for a void*). You'll have to use a switch based on type to do that...

switch( type )
{
case 'd': sprintf( newline, ln, *(int*)value ); break;
case 's': sprintf( newline, ln, *(char*)value ); break;
}

If you are using C++ you can do it alot easier by overloading the function for different pointer types and get rid of void* completely.



"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

[edited by - Paradigm Shifter on January 14, 2003 11:10:19 AM]
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Structural
Structural
What I''m trying to do is store an arbitrary value in a string, so I can write it to a file in one go.

First, I set up the start of the line with the sprintf. Preset is the start of the line:

sprintf(ln,"%s %%%c",preset,type); //replace the ''%c'' in that string with VALUE
//ln will be "PRESET %VALUE"
//now ln has a % format string... that''s where we wanna put the arbitrary value behind the void pointer

sprintf(newline,ln,*value); //ln contains a format string, which will be replaced with the value behind the pointer (so I hoped)

OK, the cast works, I had hoped for a shorter solution, but hey, you can''t have everything

Thanks!!
STOP THE PLANET!! I WANT TO GET OFF!!
Paradigm Shifter
Paradigm Shifter
C++ overloading is a much nicer approach...


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
civguy
civguy
quote:
Original post by Structural
you can''t have everything
Perhaps not, but this you can if you learn a bit C++.

  
#include <fstream>

int main() {
float a = 2.03f;
int b = 2;
std::ofstream out("file.txt");
out << a;
out << b;
}

See how it doesn''t differ at all whether you put floats or ints to the file

And in your original code, you could''ve had:

  
template <typename T>
void setParam(char* filename, char* preset, const T& value, char type)
{
char ln[100];
char newline[100];

sprintf(ln, "%s %%%c", preset, type); //result: NEWLINE = %

sprintf(newline, ln, value); //result: NEWLINE = NEWLINE, with % replaced


Which is still clumsy because you have to pass the ''type'' argument.
Structural
Structural
damn... some of the first things I learned in my C courses, and I forgot completely.
STOP THE PLANET!! I WANT TO GET OFF!!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.