How to share a variable between two functions in C

2020-04-14 07:40发布

In C, suppose var1 is a variable in foo1() and foo2() wants to access var1, however, foo1() doesn't call foo2(), so we can't pass it by parameter. At the same time, only foo1() and foo2() will access it, so I wouldn't like to declare it as global variable. It will be similar with the "friend semantics" in c++, is there any way to implement this in C?

void foo1() {
  ... 
  var1;
  ....
}


void foo2() {
 ...
 how to access var1?
 ...
}

标签: c function
6条回答
家丑人穷心不美
2楼-- · 2020-04-14 08:30

you pass the variable to both functions.... in general functions shouldn't hold state.

quickly you find passing variables is not so nice and becomes fragile, so instead, you pass structs.... then functions start working on the state of structs.

typedef struct 
{
    int var1;
} blah_t;

void foo1(blah_t* b)
{
    b->var1=0;
}

void foo2(blah_t* b)
{
    b->var1++;
}

this is the very simplistic seed idea behind doing OO C.

查看更多
地球回转人心会变
3楼-- · 2020-04-14 08:30

You need to declare var1 outside the scope of the functions and then send it as a parameter to both. Alternatively, declare it as a global variable.

查看更多
相关推荐>>
4楼-- · 2020-04-14 08:41

Regarding similar with the "friend semantics" in c++. C does not have the same capability.
Regarding so we can't pass it by parameter

The only option in C for accessing a variable from function to function without passing as a function parameter is to use some type of global scope variable.

In the event void foo1() and void foo2() exist in different C modules...
but you still want to be able to access the same variable, and ensure its value is the same at all times, in all places within your project, then consider using extern scope:

Within a header file that is common to both (multiple) modules, a project scope global can be implemented as follows.

file.h

void foo1(void);
void foo2(void);
extern int var1;  

file1.c

#include "file.h"
int var1 = 5; //in only 1 module, declare and initialize the 
              //extern defined in the common header -file.h-

int main(void)
{
    printf("value of var1 is %d\n", var1);//original value of var1
    foo1();
    printf("value of var1 is %d\n", var1);//var1 modified by foo1()
    foo2();
    printf("value of var1 is %d\n", var1);//var1 modified by foo2()
    return 0;
}

void foo1(void)
{
    var1 = 15;//Now that the project global variable
              //has already been declared and defined, it can simply
              //be used, in this file...   
}

file2.c

#include "file.h"

void foo2(void)
{
    var1 = 20;... and in this one
}
查看更多
家丑人穷心不美
5楼-- · 2020-04-14 08:41

No. The variable only exists on the function stack while foo1() is running. The stack will vanish when leaving the function. You could make the variable static to keep it alive, but then you can't access it from the outside either without hacks.

查看更多
做个烂人
6楼-- · 2020-04-14 08:43

by reference is one way: (in this example the memory for i is local to caller())

void caller()
{
    int i = 5;
    foo(&i);
    bar(&i);
    printf("\n final i is %d",i);
}

void foo(int *i)
{
    printf("%d",*i);
    *i += 5;
}

void bar (int *i)
{
    printf("%d",*i);
    *i += 5;
}

global: (usually considered horrible i would have a name more like GLOBAL_I or something)

int i = 0;

void caller()
{
   i=5;
   foo();
   bar();
   printf("\n final i is %d",i);
}

void foo()
{
   printf("%d",i);
   i += 5;
}

 void bar (int i)
 {
      printf("%d",i);
      i += 5;
 }
查看更多
7楼-- · 2020-04-14 08:43

This answer is inspired by the 'Module' concept, found in many other languages, which can be approximated using gcc's nested functions. Variable var1 is within scope for both foo1() and foo2(), but is out of scope for everything else. The solution uses neither global vars nor parameters.

void foo(int fn)
{
    static int var1;

    void fn1(void)
    {
        var1 = 15;
    }

    void fn2(void)
    {
        var1 = 20;
    }

    (fn == 1)? fn1(): fn2();
    printf("Value of var1 is now %d\n", var1);
}

void foo1(void){foo(1);}
void foo2(void){foo(2);}

int main (void)
{
    foo1();
    // Expected stdout: Value of var1 is now 15

    foo2();
    // Expected stdout: Value of var1 is now 20
}  
查看更多
登录 后发表回答