tags:

views:

37

answers:

3

Hi can we hide variables in a structure from outside of the given file using static keyword??? similarly can we hide a global variable from outside of the given file using static keyword? if so please let me know how. Thanks in advance...

+1  A: 

You can hide a global variable from outside a file using the static keyword, but you cannot hide a subset or a single variable of a structure.

Maurizio Reginelli
+3  A: 

Use an opaque data type, where the type is declared in a header, but the struct is defined in the implementation. This means any code which includes the header can pass pointers around to the type, but only the implementation can modify and process data contained by the struct.

The typical example is FILE from stdio.h.

For more information see http://en.wikipedia.org/wiki/Opaque_pointer

James Morris
awesome thank you soo muchhh
Vineel Kumar Reddy
Also, with regard to the use of static, one very good use of it is for private functions within the implementation so **nothing** outside of the implementation can use the functions. For example, you might create two or more API functions to create your data in subtly different ways, but each of these functions (defined in the implementation) calls the private/static function to create the data in an implementation specific way.
James Morris
This btw is how static inline function works: by placing them in a header file, even if the compiler decides not to make them static, you obtain one copy of the executable code for each object file, but since they are declared as static, during the linking phase they are not exported, so you don't get linking errors.
Dacav
+1  A: 

By means of the static keyboard you typically make private a function. If you use it for a variable, both global or local with respect to a function, you obtain a non-reentrant code.

I strongly suggest you to avoid this, because gives you troubles in a multithreaded environment. Also you may not be interested in multithreading programming, but you don't know what you'll need in future!

On your specific issue, I totally agree James Morris advice about opaque pointer.

Dacav