initializing to expressions

Brian Fennell fenn at wpi.WPI.EDU
Tue Feb 12 03:06:16 AEST 1991


In article <1991Feb11.005518.3989 at watdragon.waterloo.edu> 
	dsebbo at dahlia.uwaterloo.ca (David Ebbo) writes:
>In article <91039.144030BRL102 at psuvm.psu.edu> 
>	BRL102 at psuvm.psu.edu (Ben Liblit) writes:
>>I want to initialize an array with values that are constant, but are in the
>>form of expressions.  For example,
>>
>>   double  value[ 2 ] = { sqrt( 2 ) / 2, cos ( sqrt( 5 ) ) };
>
>I think that the answer is no.  To be able to use initialization, the values

Althogh I would have to aggree that the answer is no to your specific
question I suggest something like this.

---------------makeconsts.c--------------
#include <math.h>
#include <stdio.h>

#define CONNOTOPEN	1	/* non zero for errors */
#define	GOOD		0	/* zero for everything-fine */

#define _		fprintf
#define begin 		_(F,"\n\t{\n\t")
#define element(d) 	_(F,"%21G",(double)(d));
#define comma		_(F,"\t , \n\t")
#define end 		_(F,"\n\t} ;\n\n")


static char CONSTDATA[]="./constdata.h"
FILE	*F;

main(argc,argv) int argc;  char *argv[];
{
  if ( ! (F = fopen(CONSTDATA,"w")) ) {
    printf("%s: Cannot Open %s\n",argv[0],CONSTDATA);
    exit( CANNOTOPEN );
  }

  _(F,"	/*********************************************\\      	\n");
  _(F,"	  %s created by %s.c  \n",CONSTDATA,argv[0]);
  _(F,"	                  D O   N O T                       	\n");
  _(F,"	   C H A N G E   T H I S   B Y   H A N D !          	\n");
  _(F,"	\\*********************************************/ 	\n");
  _(F,"\n\n");

  _(F,"static double  value[]="		); 	open;
  element( sqrt( 2 ) / 2 		); 	comma;
  element( cos ( sqrt( 5 ) )		); 	end;

/*
	.
  	.
  	.
*/

exit (GOOD);
}

---------Makefile--------------
all: myprog


makeconsts.o: makeconsts.c


makeconsts: makeconsts.o
	cc -o makeconsts makeconsts.o -lm

constdata.h: makeconsts
	makeconsts

myprog: myprog.o constdata.h
	cc -o myprog myprog.o -lm



More information about the Comp.lang.c mailing list