[comp.lang.c] Declaring externally-defined structures

bertsche@llnl.gov (Kirk Bertsche) (05/30/91)

I am new to C programming, and am having some difficulty trying to declare 
a variable in a function, which is part of a structure defined in my 
top-level routine.  I have a main program with about 6 functions, which is 
to be patched into an existing software system.
My top-level routine (containing a function and my main program) has, near 
the top:

    struct lb_link tuneflag, tuneerror;

In the main program I have;

        if (!rsv_1name("STUNE.FLG", LB_INPUT, &tuneflag)) high_water(98);
        if (!rsv_1name("STUNE.ERR", LB_INPUT, &tuneerror)) high_water(98);

(This uses a bunch of routines defined by the existing software to resolve 
names and assign addresses and structures.)
When I want to read the state of these variables, I do something like:

    while (*tuneflag.pval) {

This all seems to work fine in my top-level routine.


The problem I'm having is when I want to check this flag in another 
function.  I've tried all of the following near the beginning of the 
second function (on line 22, to be precise):

    extern struct lb_link tuneflag,tuneerror;
    struct lb_link tuneflag,tuneerror;
    extern struct tuneflag,tuneerror;
    extern float *tuneflag.pval,*tuneerror.pval;

But none of these seem to work.  When I try to check the flag, by looking 
at *tuneflag.pval (on line 40), I get compiler error messages:

line 40: pval undefined
line 40: member of structure or union required
line 40: incorrect indirection

For some of the attempts above, I also get errors like:

line 22: unknown size
line 22: unknown size

Any ideas?  How can I tell this function that the variable in question is 
defined elsewhere?
Please respond by e-mail, (bertsche@llnl.gov), if anyone has any ideas.  
Thanks!

-----------------------------------------------------------------------------
Kirk Bertsche        (bertsche@llnl.gov)        (415) 422-8139
Lawrence Livermore National Laboratory
P.O. Box 808  L-397, Livermore, CA 94551-0808

gordon@osiris.cso.uiuc.edu (John Gordon) (05/30/91)

	What you want is a global variable.  To make a variable global, 
declare it as normal, but do so outside of the functions.  Most people
put them at the top, after the #includes and #defines.  Example:

----------------------------------------
#include <stdio.h>

#define MAX 100

int gack; /* global! */

main()
{
...
...
}
-----------------------------------------

	Any function in the above file can use the global variable "gack". 


---
John Gordon
Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>