How to convert char []/int into void *? newbie question

B

BNP

Hi All,

I have follwoing definitions:

#define MAX_PARAMS 256
typedef struct
{
char funcName[128];
int numParams;
void *params[MAX_PARAMS];
}ML_Command;

int Execute (ML_Command cmd)
{
.....
.....
strcpy (cmd.funName, "SavePortInfo");
cmd.numParams = 5;
cmd.params [0] = (void *)sPortName; //char sPortName[]
cmd.params [1] = (void *)iBdRate; //int iBaudRate


errorCode= ML::Execute (cmd);//this a static function in ML class
return errorCode;
}

The question here is how do I convert sPortName and iBdRate in void*.
Are the cast I have written in code right? If it is wrong, please tell
me where I am making mistake?

Thanks

BNP
 
W

Walter Roberson

I have follwoing definitions:
errorCode= ML::Execute (cmd);//this a static function in ML class

That isn't C, that's C++. C++ is a distinct language with
different semantics. If you don't want to receive joke answers,
I would recommend you take the question to comp.lang.c++ .
 
M

MJ

Hi
The typecasting done over here is Ok.
But you have to be very careful while accessing the void pointer array
As you need to again type cast void pointer to the required type to
access the element or the char ..
for ex
cmd.parmas[1] is storing interger value and you have typecasted the int
pointer to void pointer. so while accessing the
int temp = (int *) cmd.params[1] ;
you have to type cast it properly otherwise it will give wrong results

MJ
I
 
M

Michael Mair

BNP said:
Hi All,

I have follwoing definitions:

#define MAX_PARAMS 256
typedef struct
{
char funcName[128];
int numParams;
void *params[MAX_PARAMS];
}ML_Command;

int Execute (ML_Command cmd)
{
....
....
strcpy (cmd.funName, "SavePortInfo");
cmd.numParams = 5;
cmd.params [0] = (void *)sPortName; //char sPortName[]
cmd.params [1] = (void *)iBdRate; //int iBaudRate


errorCode= ML::Execute (cmd);//this a static function in ML class
return errorCode;
}

The question here is how do I convert sPortName and iBdRate in void*.
Are the cast I have written in code right? If it is wrong, please tell
me where I am making mistake?

Pointers are used to store addresses.
"void *" is a pointer type.
So, to arrive at something meaningful, you should assign addresses to
the elements of cmd.params.
The first assignment does that (sPortName decays into &sPortName[0]),
the second doesn't. Use cmd.params[1] = &iBdRate; instead.

Apart from that, you seem to program in C++. There, you also need
the cast. (However, in C++ there are better ways to do the things
you want to do.)
You can get better advice about C++ in a newsgroup dedicated to C++,
e.g. comp.lang.c++

If you want to learn C, then I suggest you have a look at the FAQ:
http://www.eskimo.com/~scs/C-faq/top.html


Cheers
Michael
 
K

Keith Thompson

BNP said:
cmd.params [1] = (void *)iBdRate; //int iBaudRate

If iBdRate is an int, converting it to void* is not guaranteed to
work. Types int and void* are not guaranteed to be the same size;
either could be bigger than the other. The cast isn't illegal, but
it's non-portable. (That's true for C; I'm reasonably sure it's also
true for C++.)

When you post to comp.lang.c++, be sure to tell them what you're
really trying to accomplish. Converting the int to void* presumably
is a means to an end, not an end in itself. There's probably a better
way to do whatever you're trying to do, but we can't tell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top