[comp.lang.c] casting ints and floats to char*

robert@nereid.jpl.nasa.gov (Robert Angelino) (04/25/91)

Hello C people,
	I'm trying to have a char* array point to different types, i.e
hold addresses of ints,floats, and longs.

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]));
}

I hope I'm conveying myself clearly--that's what I'm trying to do.

Does anyone have any suggestions??

thanks in advance

p.s. Please e-mail me directly.

-- 
    -     ------       -                              Robert Angelino
   | |   | ----  \    | |                             ms T-1704L
   | |   | |   \ |    | |                             4800 Oak Grove Drive
   | |   | | --  |    | |                             Pasadena, CA 91109
---| |   | | \__/     | |___                          robert@triton.jpl.nasa.gov
\____|et |_|ropulsion |_____\aboratory                (818) 354-9574

worley@compass.com (Dale Worley) (04/25/91)

In article <6185@mahendo.Jpl.Nasa.Gov> robert@nereid.jpl.nasa.gov (Robert Angelino) writes:

   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]));
   }

Better would be to define ptr as an array of void *, since that's
the new convention for "generic pointer".  (char * will also work, and
you have to use it if your compiler doesn't support void *, but it
doesn't protect you from accidentally dereferencing the pointer.)

Second, &(*ptr[0]) is equivalent to ptr[0].

But you don't want to say that, because sscanf wants three pointer
arguments: int *, long *, and double *, each of which may be
represented in a different manner than char * or void *.  What you
want to say is:

   sscanf(..., (int *)ptr[0], (long *)ptr[1], (double *)ptr[2]);

Always cast your generic pointers back to the right types before using
them to access data.

Dale

Dale Worley		Compass, Inc.			worley@compass.com
--
"Bob" sold it.  I bought it.  That settles it. -- <_Jym_R_Dobbs_>

jar@ifi.uio.no (Jo Are Rosland) (04/25/91)

In article <6185@mahendo.Jpl.Nasa.Gov> robert@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@ifi.uio.no

bhoughto@pima.intel.com (Blair P. Houghton) (04/26/91)

In article <6185@mahendo.Jpl.Nasa.Gov> robert@triton.JPL.NASA.GOV writes:
>Hello C people,
>	I'm trying to have a char* array point to different types, i.e
>hold addresses of ints,floats, and longs.
>
>I want to be able to do the following:
>
>void has_to_be_done_this_way()
>{
>char *ptr[12];

Use

    void *ptr[12];

instead.

>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]));

Use

    sscanf(buf,"%2d %5ld %15lf",(int *)ptr[0],(long *)ptr[1],(double *)ptr[2]);

instead.

>}

You might also look into declaring an array of unions rather
than one of pointers.

				--Blair
				  "If you want a more perfect union,
				   talk to Jefferson and Madison..."