casting ints and floats to char*

Blair P. Houghton bhoughto at pima.intel.com
Fri Apr 26 12:46:08 AEST 1991


In article <6185 at mahendo.Jpl.Nasa.Gov> robert at 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..."



More information about the Comp.lang.c mailing list