Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!bu.edu!m2c!wpi.WPI.EDU!fenn From: fenn@wpi.WPI.EDU (Brian Fennell) Newsgroups: comp.lang.c Subject: Re: initializing to expressions Message-ID: <1991Feb11.160616.2747@wpi.WPI.EDU> Date: 11 Feb 91 16:06:16 GMT References: <91039.144030BRL102@psuvm.psu.edu> <1991Feb11.005518.3989@watdragon.waterloo.edu> Organization: Worcester Polytechnic Institute Lines: 73 In article <1991Feb11.005518.3989@watdragon.waterloo.edu> dsebbo@dahlia.uwaterloo.ca (David Ebbo) writes: >In article <91039.144030BRL102@psuvm.psu.edu> > BRL102@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 #include #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