Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to hide the structure in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how the C language hides the structure", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "how C language hides the structure" article.

In C++, we can still use classes to replace structures, but in C language, there are no classes, we can only use structures, but very often, we need to hide the fields of structures so that they are not directly accessed by outsiders. Instead, we have indirect access through the functions we write, which improves the encapsulation of the program.

The implementation method, to put it simply, is that when the structure is defined, it is defined in the .c file, and then we define some functions that access the structure. In the .h file, only the function prototype declaration and the declaration of the structure are stored.

Look at an example.

In .c file

/ / stu.c#include # include # include struct stu {char id [10]; int score;}; struct stu * new_stu () {struct stu * s; s = (struct stu *) malloc (sizeof (struct stu)); return s;} void set_id (struct stu * s Char * id) {strcpy (s-> id,id);} char * get_id (struct stu * s) {return s-> id;}

As you can see, in the .c file, I defined a structure and defined some functions to manipulate it.

In the .h file

Stu.h#ifndef STU_H#define STU_Hstruct stu;extern void set_id (struct stu * s id); extern char * get_id (struct stu * s); extern struct stu * new_stu (); # endif

In .h I declared the structure struct stu and wrote a prototype declaration of the function for other files to call.

I quoted stu.h in main.c

Here's main.c.

# include # include "stu.h" int main () {/ / struct stu s; / / s.score = 100; / / struct stu s = {{0}}; struct stu * s; s = new_stu (); set_id (s, "950621"); char * id = NULL; id = get_id (s); printf ("id set to:% s\ n", id); return 0;}

As you can see, in the main function, I first define a pointer of type struct stu, then allocate space to the pointer through new_stu (), and manipulate it through the other two functions.

Here we need to pay attention to the part I commented out and explain it:

In this case, you cannot define a variable of type struct stu!

Because:

In the .h file, only the structure is declared, and there is no description of the details of the structure, that is, only a struct stu is declared in main.c, so that the compiler knows that there is a structure type called struct stu, but it does not know the internal details of stu.

As we all know, when defining a variable, the compiler allocates memory space to it, but at this time, the compiler does not know the internal details of stu, and it does not know how much space the variables in the structure of stu take up, so it is naturally unable to allocate memory. In this way, an error will be reported during compilation.

But defining a pointer variable is different. No matter what type of pointer it is, it takes up 4 bytes of memory. The compiler only needs to make sure that there is a type called struct stu, and the declaration in .h is telling the compiler that there is such a type.

At the same time, the fields of the structure cannot be accessed in this case, for example, the statement s-> score=100; will report an error at compile time, because, as above, the compiler does not know the internal details of the struct stu structure.

Through the above method, in files other than the stu.c file, you can only manipulate the structure body variable indirectly through the functions defined in stu.c, but not directly, including not creating a structure volume variable!

This not only reflects the encapsulation of the program, but also improves the security of the program. But we need to write a lot of operation functions, including the function of creating structure pointer variables to allocate space.

We can also declare the pointer type of a structure with typedef in the .h file, such as typedef struct sut * pStu

The above is the content of this article on "how C language hides the structure". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report