Using stderr in an initialization?

Steve Kargl sgk at troutmask.apl.washington.edu
Fri May 2 20:27:25 UTC 2008


I'm porting a piece of code to FreeBSD, and I've run into
a problem that I currently don't know how to solve. I scanned
both the Porter's Handbook and the Developer's Handbook, but
came up empty.

A reduce testcase is

#include <stdio.h>

typedef FILE *FILEP;

static FILEP outfile = {stderr};

int
main(int argc, char *argv[])
{
	FILE *fp;

	if (argc == 2) {
		fp = fopen(*++argv, "w");
		outfile = fp;
	}

	fprintf(outfile, "Ouch!\n");

	return 0;
}

GCC gives

troutmask:sgk[204] cc -o z a.c
a.c:5: error: initializer element is not constant
a.c:5: error: (near initialization for 'outfile')

Yes, I know in the simple example above that I could
put 'outfile = stderr' above the 'if (argc == 2)'
statement.  In the much more complicated code, it isn't
clear where such a change be made.  So, anyone have a
suggestion on how to change line 5 to satisfy gcc?

 

-- 
Steve


More information about the freebsd-ports mailing list