2

Can some one please explain How you will access an static variable from other C/ C ++ files?

  • 7
    static variables have file (or shorter) scope so are not intended to be accessed from other files. Can you edit your question to include code that demonstrates what you're trying to do here? – simonc Sep 30 '13 at 11:01
  • possible duplicate of [What does "static" mean in a C program?](http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program) – Camille G. Sep 30 '13 at 11:01
  • @CamilleG. no its not a duplicate. – Megharaj Sep 30 '13 at 11:09
  • @Megharaj And what is new in that question that does not have been asked or answered in the given duplicate ? – Camille G. Sep 30 '13 at 11:13
  • @CamilleG. passing the static variable to the functions in the other files. – Megharaj Sep 30 '13 at 11:18

4 Answers4

2

static means different things in different contexts, so I'm not sure exactly what you mean. But assuming you mean a top-level-scoped variable marked as static, the answer is that there's no reliable way to access it directly from other files --- marking it as static in fact is exactly how you make sure of that.

If you want to grant restricted access to a static variable from other files, provide a function or set of functions with signatures declared in an .h file and definitions in the same file as the static variable that read or manipulate the variable as desired.

If you want to grant unrestricted access to the static variable, then, uh, just remove the static keyword, since you want the opposite of what static means.

jacobm
  • 13,790
  • 1
  • 25
  • 27
1

One way is, pass the address of the static variable to the function in the other file. This is one of the possible way.

Megharaj
  • 1,589
  • 2
  • 20
  • 32
0

Declaring an identifier (name of an object) with static limits its scope and gives it internal or no linkage. Because the name does not have external linkage, the same identifier in other source files (compiled separately) cannot be linked to the name.

This means that other source files cannot use the identifier to access the object. The only way to access an object without its name is by address. So other source files can directly access an identifier declared static only if they are given its address in some means, such as in a function parameter or a data structure.

Other source files might indirectly access an object through helper functions that read or write the object’s value or that provide other services related to the object.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
-3

You can create a .h file and create your static variable in it. You will then have to include that .h file into all other files where you want to use it. Keep that in mind that the static behavior will not be achieved!

  • Previous comment removed. You're quite correct that this will build; it just won't work the way the OP expects as (s)he'll have multiple file-local variables that won't share state. If this is what you mean by the last sentence of your answer, it could be made much clearer. – simonc Sep 30 '13 at 11:11
  • @simonc will it work if we pass the address of the static variable to functions in other file. By curiosity – Megharaj Sep 30 '13 at 11:12
  • @Megharaj Agreed. That isn't the approach this answer is suggesting however. – simonc Sep 30 '13 at 11:13