【问题标题】:How to share a variable between two functions in C?如何在 C 中的两个函数之间共享一个变量?
【发布时间】:2013-11-27 22:11:21
【问题描述】:

在C语言中,假设var1是foo1()中的一个变量,foo2()想访问var1,但是foo1()没有调用foo2(),所以我们不能通过参数传递。同时,只有 foo1() 和 foo2() 会访问它,所以我不想将它声明为全局变量。与c++中的“朋友语义”类似,有什么方法可以在C中实现吗?

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


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

【问题讨论】:

  • “朋友”的想法与类和私有/受保护的成员有关。在函数中,您只能使用全局变量。 codingunit.com/c-tutorial-functions-and-global-local-variables
  • 还有另一种方式,没有人在他们的回答中强调,它是静态全局的概念,我通常不会推荐它,但是在 C 中,在 C 文件中,如果你声明静态整数变量1;它仅对该文件中的函数可见。所以如果你有 foos.c 和这两个 foo 函数,两者都可以访问 var1,文件之外的任何东西都不能使用它。仍然不好,但它限制了全局的范围。基本上在 C 中,public/private/friends 不是一个受支持的概念,它基本上只是一个大狂欢,任何人都可以戳别人的位。
  • @KeithNicholas 是的。我认为静态全局会做。
  • 我建议一般不要使用它,它只是那些经常使 C 代码难以维护/更改的事情之一。
  • @ryyker 我绝对不建议这样做。如果您想要那种东西,我建议将静态全局范围限定为文件,然后将“getter 函数” int getVar1() 放入返回静态全局的头文件中。

标签: c function


【解决方案1】:

你将变量传递给两个函数....一般函数不应该保持状态。

很快你就会发现传递变量不是很好并且变得脆弱,所以你传递结构......然后函数开始处理结构的状态。

typedef struct 
{
    int var1;
} blah_t;

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

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

这是做 OO C 背后非常简单的种子想法。

【讨论】:

  • 我认为 blah_t 是保留的......我猜只在 POSIX 中。
【解决方案2】:

您需要在函数范围之外声明var1,然后将其作为参数发送给两者。或者,将其声明为全局变量。

【讨论】:

    【解决方案3】:

    通过引用是一种方式:(在此示例中,i 的内存是 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;
    }
    

    全局:(通常被认为是可怕的i 的名字更像GLOBAL_I 或其他东西)

    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;
     }
    

    【讨论】:

    • (你忘记了 i += 5 之后的分号)
    【解决方案4】:

    关于类似于c++中的“朋友语义”。 C 没有相同的能力。
    关于所以我们不能通过参数传递它

    C 中的唯一选项是在不作为函数参数传递的情况下从一个函数访问一个函数到另一个函数,即使用某种类型的全局范围变量。

    如果void foo1()void foo2() 存在于不同的C 模块中...
    但是您仍然希望能够访问相同的变量,并确保其值始终在项目中的所有位置都相同,然后考虑使用extern scope

    在两个(多个)模块通用的头文件中,项目范围全局可以如下实现。

    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】:

      没有。该变量仅在 foo1() 运行时存在于函数堆栈中。离开函数时堆栈将消失。您可以将变量设置为静态以使其保持活动状态,但是如果没有 hack,您也无法从外部访问它。

      【讨论】:

        【解决方案6】:

        这个答案的灵感来自许多其他语言中的“模块”概念,可以使用 gcc 的嵌套函数来近似。变量var1foo1()foo2() 的范围内,但不在其他所有范围内。该解决方案既不使用全局变量也不使用参数。

        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
        }  
        

        【讨论】:

        • 你确定这条线foo((fn == 1)? fn1(): fn2()); 吗?
        • 你编辑了你的答案,这很好,但即便如此你有variable ‘var1’ set but not used |
        • @Michi - 是的,代码只是回答了最初的问题,即 2 个函数可以独占“看到”一个公共变量,但其他函数不能。但是对于您,我将添加一个实际使用 var1 的 printf()。
        • 这真是一个糟糕的模式。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-11
        • 1970-01-01
        • 2019-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多