[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::decc

Title:DECC
Notice:General DEC C discussions
Moderator:TLE::D_SMITHNTE
Created:Fri Nov 13 1992
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2212
Total number of notes:11045

2186.0. "stdin/stdout to initialize global variable?" by CSC32::J_HENSON (Don't get even, get ahead!) Tue May 13 1997 16:53

DEC C, OpenVMS ALpha, Port from Unix.

Rather than paraphrase my customer's question, I'm posting it as is.

Is this possible to do?  I can't figure out how if it is.

Thanks,

Jerry

=======================================================

I am trying to port some code from Unix to Open VMS (on an Alpha), and I am
having trouble initializing a static FILE * to stdout.  Below is an example
program and the compiler error message.  Is it possible to use stdin and
stdout to initialize a global variable?


$ type hello.c
#include <stdio.h>

struct my_struct {
  FILE *test;
};
struct my_struct tests = {
  stdout
};

main()
{
  fprintf(tests.test,"Hello world\n");
}
$cc hello.c

  stdout
..^
%CC-E-NEEDCONSTEXPR, In the initializer for tests.test, "decc$ga_stdout" is
not
constant, but occurs in a context that requires a constant expression.
at line number 8 in file RUSER5:[STAFF.EPPERLY.TMP]HELLO.C;2
T.RTitleUserPersonal
Name
DateLines
2186.1DECCXL::WIBECANThat&#039;s the way it is, in Engineering!Tue May 13 1997 17:5219
In short, no.  On VMS, stdout is defined as

	#define stdout (decc$ga_stdout)
	extern FILE * stdout;

stdout is defined as an external pointer-to-FILE, and the value contained in it
cannot be determined at compile time.  Compare the definition on Unix:

	extern FILE _iob[];
	#define stdout (&_iob[1])

Here, stdout is defined as the address of an external FILE, rather than an
external pointer-to-FILE.  The compiler can initialize to an external address,
just not to the value contained in that address.

There are ways around it.  Probably the easiest is to initialize the pointer at
run time.

						Brian
2186.2SPECXN::DERAMODan D&#039;EramoTue May 13 1997 21:5629
	Where the standard describes the contents of <stdio.h>
        (section 4.9 of X3.159-1989) it states
        
>        		stderr
>        		stdin
>        		stdout
>
>        	which are expressions of type "pointer to FILE" that
>        	point to the FILE objects associated, respectively,
>        	with the standard error, input, and output streams.
        
        It is unportable to assume any more than this.  For example,
        code that assumes these are compile time constant expressions
        
        	static FILE *fp = stdout;
        
        and code that assumes these are lvalues
        
        	void f(FILE **);
        	void g(void) { f(&stderr); }
        
        and code that assumes that these are modifiable lvalues
        
        	stdin = fopen(filename, "r"); /* that's what freopen is for! */
        
        all lack portability because a particular implementation need
        not be implemented so that these work.
        
        Dan
2186.3ThanksCSC32::J_HENSONDon&#039;t get even, get ahead!Wed May 14 1997 10:383
Thanks.  I'll pass this on to my customer.

Jerry