从一个文件中的静态变量的访问到另一个文件 [英] Access of static variable from one file to another file

查看:200
本文介绍了从一个文件中的静态变量的访问到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到像如何访问,宣布在file1.c中静态到另一个file2.c中?

变量的问题就来了

是否有可能访问静态变量?

我对用C static关键字的理解是,

静态是内部链接,所以他们只能从一个编译单元访问 - 在那里他们被定义的。具有内部链接的声明对象专用于单个模块。

由于我的一个朋友建议我下面的解决方案。

file1.c中

 的#include<&stdio.h中GT;   诠释的main()
   {
          INT B = foo的();
          的printf(%D,B);
          返回0;
   }

file2.c中

 静态INT A = 25;   INT富()
   {
        返回;
   }

编译GCC在file1.c file2.c中-o文件

如果我做以上我可以访问变量。

所以我的问题是:


  1. 请问上面的程序违反静态变量规则?


  2. 如果没有,为什么会这样,以及是否有任何其他方式来访问静态变量除了包括文件(的#include< ...> )不是这样的。

    <一个href=\"http://stackoverflow.com/questions/2311249/how-am-i-able-to-access-a-static-variable-from-another-file\">How我能够从另一个文件访问静态变量?

    <一个href=\"http://stackoverflow.com/questions/1046546/in-c-how-do-i-restrict-the-scope-of-a-global-variable-to-the-file-in-which-its\">In C,我怎么限制一个全局变量的范围,在它的声明的文件?


  3. 纠正我,如果我错了静态变量的概念,如果没有更好的解决方案可访问静态变量?



解决方案

1)没有上述程序违反静态变量规则?

没有你不vailoting任何规则。在这里,富功能创建的静态变量的值复制,并在其他文件中使用。其优良的。

2)如果没有,为什么会这样,而且还有其他的方式来访问静态变量除了包括文件(#包括&LT;>)不喜欢这样的我怎么能够从另一个文件访问静态变量?

中的静态变量只意味着只有文件中使用。

您不能使用这个变量使得它们的extern的其他文件。

另外一个肮脏的黑客就是获得一个静态变量的指针,并作出这样的全球指针和制作,作为在另一个文件中的extern您可以使用静态变量。

在file1.c

 #包括LT&;&stdio.h中GT;
  静态INT A = 25;
  为int * PTR =安培; A;

file2.c中

 #包括LT&;&stdio.h中GT;
EXTERN为int * PTR;   诠释的main()
   {
          的printf(%d个,* PTR);
          返回0;
   }

纠正我,如果我错了静态变量的概念,如果没有更好的办法有哪些?


  1. 一个静态变量一生跨越计划的整个运行扩展


  2. 如果您不同意有些值初始化静态变量那么它的默认值是0。


  3. 一个静态变量的范围仅限于它的文件。你可以叫不上名字来自一个不同的文件访问。


  4. 您有temp1.c文件和temp2.​​c都越来越一起编译,那么你也可以拥有同一个名字的静态变量在这两个文件 - 他们是独立的变量


在C中,如何限制一个全局变量来在它的声明文件范围有多大?

只要制作全局变量为static。

I recently came across the question like how to access a variable which declared static in file1.c to another file2.c?

Is it possible to access static variable?

My understanding about static keyword in C is,

static is "internal linkage", so they are accessible only from one compilation unit - the one where they were defined. Objects declared with internal linkage are private to single module.

As one of my friend suggest me below solution.

In file1.c

   #include <stdio.h>

   int main()
   {
          int b=foo();
          printf("%d",b);
          return 0;
   }

in file2.c

   static int a=25;

   int foo()
   {
        return a;
   }

compiled by gcc file1.c file2.c -o file

If I do above I can access the variable.

So my questions are:

  1. Does the above program violate static variable rules?

  2. If not, why is this so, and is there any other way to access static variable except including file (#include <…>) not like this.

    How am I able to access a static variable from another file?

    In C, how do I restrict the scope of a global variable to the file in which it's declared?

  3. Correct me if I'm wrong with static variable concept and if any better solutions are available to access static variable?

解决方案

1) does the above program violate static variable rules?

No you are not vailoting any rules. Here foo function create copy of value of that static variable and used in other file. Its fine.

2) If not why is this so, and is there any other way to access static variable except including file (#include<>) not like this How am I able to access a static variable from another file?

Static variable are only mean to use in that file only.

You can not use that variable making them extern in other files.

Another dirty hack is to get pointer of that static variable and make that as global pointer and making that as extern in another file you can use that static variable.

file1.c

 #include<stdio.h>
  static int a=25;
  int* ptr = &a;

file2.c

#include<stdio.h>
extern int *ptr;

   int main()
   {
          printf("%d",*ptr);
          return 0;
   }

Correct me if I'm wrong with static variable concept and if any better solutions are available?

  1. A static variable has a lifetime extends across the entire run of the program

  2. If you do not initialize static variable with some value then its default value would be 0.

  3. A static variable has scope limited to its file only. You can not access it by name from a different file.

  4. You have temp1.c and temp2.c both are getting compiled together then also you can have static variable of same name in both files — and they are separate variables.

In C, how do I restrict the scope of a global variable to the file in which it's declared?

By making that global variable as static.

这篇关于从一个文件中的静态变量的访问到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆