stdio is not constant 
Author Message
 stdio is not constant

Hello,

I am trying to convert some old, K&R C program into
modern ANSI compliant code.

The problem I am having is with pars of the code which
assumes that 'stdin', 'stdout' and 'stderr' are constant.
One example is the following:

        static struct {
                char *sfn;
                FILE *sfd;
        } stdfiles[] = {
                {"STDIN", stdin},
                {"STDOUT", stdout},
                {"STDERR", stderr}
        };

Since 'std...' are not constants they cannot be used to initialize an array.
Any useful trick to make it work w/o re-writing an entire program?

Sincerely,

Dmitri.
--



Sun, 20 Apr 2003 08:32:43 GMT  
 stdio is not constant

Quote:
>The problem I am having is with pars of the code which
>assumes that 'stdin', 'stdout' and 'stderr' are constant.
>One example is the following:

>    static struct {
>            char *sfn;
>            FILE *sfd;
>        } stdfiles[] = {
>            {"STDIN", stdin},
>            {"STDOUT", stdout},
>            {"STDERR", stderr}
>        };

>Since 'std...' are not constants they cannot be used to initialize an array.
>Any useful trick to make it work w/o re-writing an entire program?

Replace std* by NULL in the code above and initialise the sfd members
of the array at run time, before the array is used the first time.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
--



Sun, 20 Apr 2003 10:07:07 GMT  
 stdio is not constant

Quote:

>Hello,

>I am trying to convert some old, K&R C program into
>modern ANSI compliant code.

>The problem I am having is with pars of the code which
>assumes that 'stdin', 'stdout' and 'stderr' are constant.
>One example is the following:

>    static struct {
>            char *sfn;
>            FILE *sfd;
>        } stdfiles[] = {
>            {"STDIN", stdin},
>            {"STDOUT", stdout},
>            {"STDERR", stderr}
>        };

>Since 'std...' are not constants they cannot be used to initialize an array.
>Any useful trick to make it work w/o re-writing an entire program?

>Sincerely,

>Dmitri.

Add some code at the beginning of the function similar to
        stdfiles[0].sfd = stdin;

Even if the function is called repeatedly, the assignments should
consume a lot of CPU resources.

<<Remove the del for email>>
--



Tue, 22 Apr 2003 04:03:47 GMT  
 stdio is not constant

Quote:

>         static struct {
>                 char *sfn;
>                 FILE *sfd;
>         } stdfiles[] = {
>                 {"STDIN", stdin},
>                 {"STDOUT", stdout},
>                 {"STDERR", stderr}
>         };
> Since 'std...' are not constants they cannot be used to initialize an array.

But FILE ** should work, at the cost of a redirection when fetching
the FILE * handles out of the data structure.
--



Tue, 22 Apr 2003 04:20:16 GMT  
 stdio is not constant

Quote:

>>         static struct {
>>                 char *sfn;
>>                 FILE *sfd;
>>         } stdfiles[] = {
>>                 {"STDIN", stdin},
>>                 {"STDOUT", stdout},
>>                 {"STDERR", stderr}
>>         };
>> Since 'std...' are not constants they cannot be used to initialize an array.

>But FILE ** should work, at the cost of a redirection when fetching

But can you take the address of an arbitrary expression of type pointer
to FILE (which is what stdin, stdout and stderr are supposed to be)???

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
--



Tue, 22 Apr 2003 12:15:06 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. WCHAR_MAX not an integer constant

2. "initializer element is not constant"

3. initializer not constant

4. Address of auto not constant initializer?

5. string constants not allowed in #if expressions

6. initializer element is not constant

7. standard constants are not recognized on different OS

8. I am not quite understand the program asks for

9. I am not quite understand the program asks for

10. "initializer element is not constant"

11. Invalid constant subscripts not flagged

12. Why am I not getting correct position?

 

 
Powered by phpBB® Forum Software