C Private Variables Get and Set Methods

I work in C and have some variables that I don’t want to be global, but I want to get and set methods for them that can be accessed by "Globaly" outside of the file. I'm used to doing this in Java, but C is very different this way. Basically, I am looking for something that follows this pseudo-code, but I could not find anywhere with examples that I could look at.

main.c #include data.h set(b); datalog.c #include data.h get(b); data.c private int c; set(b){ c = b; } get(c){ return c; } 
+7
source share
5 answers

You make a static variable. When a global variable is created static , its scope is limited to the current file.

An example is as follows:

File name: main.c

 #include <stdio.h> #include "header.h" extern int get(); extern void set(int); int main() { set(10); printf("value = %d \n", get()); set(20); printf("value = %d \n", get()); set(30); printf("value = %d \n", get()); set(40); printf("value = %d \n", get()); return 0; } 

File Name: header.h

 #include <stdio.h> int get(void); void set(int); 

File Name: header.c

 #include "header.h" static int value = 0; int get(void) { return value; } void set(int new_value) { value = new_value; } 

Output:

 $ gcc -Wall -o main main.c header.h header.c $ ./main value = 10 value = 20 value = 30 value = 40 $ 
+10
source

If you need private variables in c, there are a number of methods that can approximate a private variable, but in C there is actually no security concept that extends to private, public, protected (as C ++ does).

C will display the name of any variable (this is a requirement in C), so you should approach it with the idea of ​​information hiding the type of the variable (dereferencing is pretty difficult).

One trick is to define the variable as void* , with the actual type of the variable known in only one .c module.

  /* somefile.h */ extern void* counter; /* somefile.c */ #include "somefile.h" int actualCounter = 0; void* counter = &actualCounter; /* otherfile.c */ #include "somefile.h" // we can see "counter", but we cannot "use" it here; because we don't have access // to the real "hidden" type of "int". 

The best way is to extend this idea with the struct keyword and create pseudo methods like

  /* person.h */ struct s_person; typedef Person struct s_person; Person* new_Person(char* name); void delete_Person(Person* person); void Person_setName(Person* person, char* name); char* Person_getName(Person* person); /* person.c */ struct s_person { char* name; }; Person* new_Person(char* name) { Person* object = (Person*)malloc(sizeof(struct s_person)); // duplicate the string for more security, otherwise constructor // could manipulate the "private" string after construction. object->name = strdup(name); return object; } void delete_Person(Person* person) { // some implementations pass a Person** to set the reference to 0 // this implementation requires that the caller sets his own references to 0 free(person->name); free(person); } void Person_setName(Person* person, char* name) { // free the old free(person->name); // duplicate the new to provide "out of simulated class" modification by malicious // name setter. person->name = strdup(name); } char* Person_getName(Person* person) { // must return a copy, otherwise one can manipulate name // from reference provided by Person_getName(...); return strdup(person->name); } /* otherfile.c */ #include "Person.h" /* Now we can hold Person "simulated objects", but we cannot */ /* manipulate their "state" without using the C simulated object */ /* methods */ int main(int argc, char** argv) { Person* bob = new_Person("bob"); printf("%s\n", Person_getName(bob)); delete_Person(bob); // critical or we hold a pointer to freed memory. bob = 0; return 0; } 

Similar methods have several options: one should have a "public structure" with a void * pointer to a "private structure". One of them is to include “methods” as function pointers in the “public structure” (a step towards supporting polymorphism), one is to actually write a complete and correct C ++ type system that tries to resolve things like C + + (class hierarchies, polymorphism, late binding, information hiding, etc.).

In principle, you can get some "object-oriented" without too much effort, but as you add additional -ornamentation functions, you will add more brand code (until it is much easier to use an object-oriented programming language).

+7
source

You can enter:

 static int c; 

Thus, ".o" will not export the variable "c".

+2
source

In your example, you can try using struct with this information. A struct is similar to a class with only public member variables (i.e. no functions). Therefore, consider the following:

 #include <stdio.h> typedef struct _somestruct { int c; } theStruct; int getC(theStruct* obj) { if(obj == NULL) return -1; return obj->c; } void setC(theStruct* obj, int val) { if(obj == NULL) return; obj->c = val; } int main() { theStruct myStruct; setC(&myStruct, 5); printf("%d\n", getC(&myStruct)); return 0; } 

As you can see, C only works with objects and functions. But to get the global variable in all files, try static int c = 0;

The example above is almost as close as you can go to the java-style convention.

+1
source
 static int c; int get(void) { return c; } int set(int n) { c = n; } 
+1
source

All Articles