Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How can we achieve data hiding in C?

Status
Not open for further replies.

sacrpio

Member level 3
Joined
May 24, 2004
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
500
How we can achieve data hiding in C.
 

Re: DATA Hiding in C.

What do you mean by hiding? Like in the C++ (object oriented) approach?

Every variable declared in a function is hided to the external world, so to say (if they are declared as static their content will remain between function calls).
You can also declare variables outside to function as being static which means they are visible only inside the file in which you have declared. Thus they are hided from the other files.

Is my answer related to your question? If not please make your question more clear.

Belsugului
 

Re: DATA Hiding in C.

What kind of data do you want to hide?

All datas defined and used inside a funtion are not seen by other subroutines, so please try to be more exact with your question.

best regards
 

Re: DATA Hiding in C.

In c also using static function u can achieve some extent of data hiding.For details you can search for the static function in c.If any issue let me know.
 

Re: DATA Hiding in C.

data hiding in C can be achieved by using two approach as follows:
1. "ordinary" approach. This way, we are not extending the compiler capabilities. This is achieved by using C data structures, pointer to function within that structure and static functions in different modules.
2. "spartan" approach. This way, we are extending the compiler capabilities to support data hiding. An example of this approach is the GTK+ toolkit. In this approach, almost every thing is declared as type void*. The impose on the data type checks is done in a sophisticated way, using macros and lots of "acrobatic" compiler tricks. You can read the Object Oriented C book by Axel-Tobias Schreiner if you are interested in this approach.
 

Re: DATA Hiding in C.

you can anti-complied this program.
 

Re: DATA Hiding in C.

You can achieve data hiding in C by doing the following.

1. Create a .h file and tell anyone that includes the file that there exists a struct.
2. Put the struct with all of its datamembers in the .c file.
3. Anyone using the .h file will see that there is a struct available but have to use the access function to use it.

By doing this you have achieved data hiding since no one can directly access your struct or any of their members.

Simple example below.

create your .h file.
------------
/* person.h */
#ifndef SPERSON_H
#define SPERSON_H

struct Person; //This line says that there _exists_ a struct called Person and does not show how it looks like.
typedef struct Person *pPerson;

pPerson createPerson(char* first_name, char* last_name, int age);
void printPerson(pPerson);
void changeSureNamePerson(pPerson, char* new_first_name);

#endif

----------

Create your .c file.

---------
/* person.c */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "person.h"

struct sPerson
{
char surename[30];
char lastname[30];
int ager;
};

pPerson createPerson(char* surename,char* lastname, int age)
{

pPerson c = malloc(sizeof(struct Person));
assert (c != NULL);
strcpy(c->surename, surename);
strcpy(c->lastname, lastname);

c->age = age;
return c;
}

void printPerson(pPerson pers)
{
printf("Last name, Sure name, Age: %s %s %d\n", pers->lastname, pers->surename, pers->age);
}

void changeSureNamePerson(pPerson p, char* new_surename)
{
strcpy(p->surename, new_surename);
}

---------------

Create the main file.

-------------

#include <stdio.h>
#include <stdlib.h>

#include "person.h"

int main(void)
{
system("cls");
/* Put your own code here... */

pPerson *pers1 = createPerson("John", "Doe", 30);
printf("pers1 = ");
printPerson(pers1);
return 0;

}

------------


Hope this answered your question.


Regards,

Hybris
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top