casting ints and floats to char*
Jo Are Rosland
jar at ifi.uio.no
Fri Apr 26 02:49:22 AEST 1991
In article <6185 at mahendo.Jpl.Nasa.Gov> robert at nereid.jpl.nasa.gov (Robert Angelino) writes:
I want to be able to do the following:
void has_to_be_done_this_way()
{
char *ptr[12];
int i;
long j;
double p;
ptr[0] = &i;
ptr[1] = &j;
ptr[2] = &p;
sscanf(buf,"%2d %5ld %15lf",&(*ptr[0]),&(*ptr[1]),&(*ptr[2]));
}
Does it have to be a char * array? How about:
1. typedef union {
int *ip;
long *lp;
double *dp;
} NUMP;
void how_about_this_way()
{
NUMP ptr[12];
int i;
long j;
double p;
ptr[0].ip = &i;
ptr[1].lp = &j;
ptr[2].dp = &p;
sscanf(buf, "%2d %5ld %15lf", ptr[0].ip, ptr[1].lp, ptr[2].dp);
}
2. typedef struct {
int i; long l; double d;
} ILD;
void son_of_how_about_this_way()
{
ILD trip[4];
sscanf(buf, "%2d %5ld %15lf", &trip[0].i, &trip[0].l, &trip[0].d);
}
Why does it "have to be done that way", anyway? Just curious...
--
Jo Are Rosland
jar at ifi.uio.no
More information about the Comp.lang.c
mailing list