Alex Rivera | Logout

C Private Variables Get and Set methods

Asked 2012-04-25T16:38:46.277
15

I am working in C, and have some variables that I don't want to be global, but I do want to have get and set methods for them that can be accessed "Globaly" outside of the file. I am used to doing this in Java, but C is very different in this manner. Basically I am looking for something that follows this pseudo Code, but I have not been able to find anywhere with examples that I might 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;
}
Edit
Report

2 Answers

10

If you want private variables in c, there are a number of techniques that can approximate a private variable, but the C language actually doesn't have a "protection" concept that extends to private, public, protected (as C++ does).

C will show the name of any variable (it's a requirement in C) so you must approach it with the idea of information hiding the type of the variable (making dereferencing quite difficult).

One trick is to define the variable as an void* with the actual variable type being 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".

A better method is to extend this idea using the struct keyword, and make pseudo-methods, like so

 /* 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);
   // duplica
answered 2012-04-25T16:45:03.190
1

By your example, you can try using some struct with this information. A struct is like a class with only public member variables (i.e. no functions). So consider something as follows

#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 works only with objects and functions. But to get a global variable across all files, try static int c = 0;

The example above is nearly as close as you can possibly get to a "java-style" convention.

answered 2012-04-25T16:48:29.230

Your Answer