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.
extern void* counter; #include "somefile.h" int actualCounter = 0; void* counter = &actualCounter; #include "somefile.h"
The best way is to extend this idea with the struct keyword and create pseudo methods like
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); struct s_person { char* name; }; Person* new_Person(char* name) { Person* object = (Person*)malloc(sizeof(struct s_person));
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).
Edwin buck
source share