Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making variables global within all "children" of a function

Status
Not open for further replies.

WebDrake

Programmer
Sep 29, 2005
106
0
0
PL
Hello all,

Suppose I have a big function F() which calls several sub-functions,

Code:
void F(int A)
{
    int B, C;
    /* B and C are defined, then, */
    f1();
    f2();
    f3();
}

Now, the integers A, B and C are not globals. But is it possible to make them act like globals for every function called inside F()? i.e. for every function called by F() to know of their existence without having to pass either the variables or pointers to them?

Cheers,

-- Joe
 
If you want them to act like globals, then why not just make them globals?
 
But that wasn't my question. I know I can make stuff globals if they need to be.

Humour me, please. I want to know if it's possible to make sub-functions be aware of variables in a parent function without passing them, and without making them globals for the whole program.
 
Well, asked that way, no. If a variable is defined within a function, it cannot be known outside that function unless it's address is passed to the other function.

Or you can play all kinds of games with pointers and such, but then you have all kinds of other possible problems (scope issues mostly).
 
OK. That's what I thought, but I thought it would be worth asking just to make sure.

Thanks very much! :)

 
It's a very interesting thing: a function knows that a variable is in existence but can't access it (no in scope and no any pointers).
Knows... What for?..
 
You could create your pointers in a structure and malloc space for the structure (allowing you to have as many copies of these as you like). Then all you have to do is pass a pointer to this structure around. Passing one single pointer parameter should not cost you much in terms of performance and would give you all the flexibility you could ask for.



Trojan.
 
Some C compilers, notably GCC, support an extension to the C language that allows you to define a function within another function. This "sub-function" can access the outer function's local variables. That might look like this:
Code:
int calc_sum (int p1, int p2, int p3)
{
    int sum = 0;

    void accumulate (int val)
    {
        sum += val;
    }

    accumulate (p1);
    accumulate (p2);
    accumulate (p3);

    return sum;
}

Again, that's an extension to C provided by at least GCC; it's not a standard feature of the langauge.


If you're interested in this topic and want to search for more info, it's called "lexical scoping" or "static scoping." Many functional programming languages use lexical scoping.
 
Putting all the related functions into a single source file, then making the variables static at file scope might work.

Code:
// this is foo.c
static int bar;

void f1 ( void ) {
  bar = 2;
}
Nothing outside of foo.c can see bar, but everything within the file can.

--
 
Thanks all for replies. :)

Putting all the related functions into a single source file, then making the variables static at file scope might work.

I don't understand how that works, quite. I thought that static meant simply that memory was allocated for the variable for the whole lifetime of the program.

I presume that what happens in this case is that separate C files are compiled to separate object files, which are then combined to form the eventual executable ..., and that keeps the "global" variables within each file separate? That's something I have never worked with, personally; I tend to just #include everything I need into the central program. Bad practice, I'm sure.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top