The C language hides the details of the structure

  • 2020-05-19 05:17:46
  • OfStack

As we all know, in the C language, the fields in the structure are accessible. Or, in C++, the main difference between a class and a struct is that the member variable in the class defaults to private, while the struct defaults to public. This 1 property of the structure, resulting in the structure of the encapsulated data, in fact, is not encapsulated, the outside can access the weight of the structure of the field.

In C++, we can still use classes to replace structs. However, there are no classes in C, so we can only use structs. However, most of the time, we need to hide the fields of structs and not let the outside world access directly, but indirectly through the functions we write, which improves the encapsulation of the program.

The way to do this, in a nutshell, is to define the struct in the.c file, and then we define some functions that access the struct, and in the.h file, we only store the function prototype declaration and the declaration of the struct.

Look at an example

c file


//stu.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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've defined a structure, and I've defined some functions that operate on that structure.

In the.h file


stu.h
#ifndef STU_H
#define STU_H

struct stu;
extern void set_id(struct stu *s,char *id);
extern char *get_id(struct stu *s);

extern struct stu *new_stu();

#endif

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

In main.c I quote stu.h

Below is the main c


#include <stdio.h>
#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(" Set up the id for :%s\n",id);
  return 0;


  
}

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

Here, I need to pay attention to the section 1 that I have commented out.

In this case, variables of type struct stu cannot be defined!!!

Because:

In main.c, only the structure is declared, and there is no detailed description of the structure. In other words, struct stu is only declared in main.c. In this way, the compiler knows that there is a structure type called struct stu, but it does not know the internal details of stu.

We all know that when we define a variable, the compiler will allocate memory space to it. However, the compiler does not know the internal details of stu, so it does not know how much space the variables of stu will take up, so it cannot allocate memory. This will cause errors at compile time.

But defining a pointer variable is different, no matter what type of pointer it is, it takes up four bytes of memory, and the compiler just needs to make sure that there's a type called struct stu, and that declaration in h tells the compiler that there's a type.

Also, you cannot access the fields of the structure in this case, for example, s- > score = 100; This statement will report an error at compile time, for the same reason as in 1 above, the compiler does not know the internal details of the struct stu structure.

In addition to the stu.c file, you can only manipulate struct variables indirectly through the functions defined in stu.c. You cannot directly manipulate struct variables, including not creating a struct variable!

This is a good reflection of the encapsulation of the program, but also improve the security of the program. But we need to write a lot of action functions, including creating struct pointer variables to allocate space.

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

This makes it possible to declare struct pointer variables with pStu in main.c.


Related articles: