1, Structure type declaration
C language has int,char and other keywords to declare data types. Similarly, a structure can also declare a structure type.
1. Definition, declaration and use:
(1) First kind
As follows:
struct example //Define a structure (keyword + structure name) { int a; char b; }; int main(void) { struct example S;//Declare a structure type variable S struct example *pS = &S;//Declare a structure pointer variable S.a = 1;//Accessing structure variables accessing internal members pS->b = 2;//Accessing internal members through structure pointers //PS: variable usage To access internal members, the pointer uses - > access return 0; }
(2) Second
Compared with the first one, it is slightly changed as follows:
struct example //Define a structure (keyword + structure name) { int a; char b; }S ,*p;//Here, the defined variables and pointers are placed at the end of the structure int main(void) { //Just call it directly below S.a = 1;//Accessing structure variables accessing internal members pS->b = 2;//Accessing internal members through structure pointers //PS: variable usage To access internal members, the pointer uses - > access return 0; }
(2) Third
typedef keyword declaration is also the most commonly used one in my projects, as follows:
//Define a structure (typedef + structure type keyword), and the example here can be omitted typedef struct example { int a; char b; }example_s;//It means to replace struct example with example_s //A structure variable S is defined, which is equivalent to struct example S example_s S; //A structure pointer variable pS is defined, which is equivalent to struct example *pS example_s *pS = &S; int main(void) { //Just call it directly below S.a = 1;//Accessing structure variables accessing internal members pS->b = 2;//The internal structure of a member is accessed through a pointer //PS: variable usage To access internal members, the pointer uses - > access return 0; }
2, About structure domain
The use of bit field greatly facilitates the development of some fields. For example, the memory of single chip microcomputer is limited, and some flag bits used internally only need one bit. At this time, bit field can be used to save the use of memory.
1: The size of the data cannot exceed the declared bit width
For example:
unsigned char i:1 Declared 1 bit,The data range that can be represented is 0-1 unsigned char j:2 Two are declared bit,The data range that can be represented is 0-3
And the number of bits declared cannot exceed the width represented by the data type, for example:
unsigned char k:9;//unsigned char can only be 8 bits at most
Another is that when the remaining space of a byte cannot store the next bit field, it will also be stored from the next unit, for example:
unsigned char i:1//i 7 bits left unsigned char j:8//j store from the next byte unit
2. Unknown bit field
Indicates that the n bits after this byte are not available, for example:
unsigned char i:1; unsigned char : 3;//Bits 1-3 of this byte are not available unsigned char j:1;//Starting from the 4th place
3. Airspace
Indicates that the n bits after this byte are not available, and the data is stored from the next byte, for example:
unsigned char i:1; unsigned char :0;//The last 7 bits of this byte are not available unsigned char j:1;//This byte is already the next byte
3, About memory alignment
In cpu, in order to speed up memory access and data reading, the basic c data types are not randomly stored in memory addresses. For different data types, there are different memory alignment requirements.
char:Start at any byte address: for example, 0 x00000001 short:Address starting from even byte: e.g. 0 x00000002 int,float:Starting from the byte address that can be divided by 4: for example, 0 x00000004 long,double:Starting from the byte address that can be divided by 8: for example, 0 x00000008
But what does this have to do with the structure? We know that the structure can contain multiple member variables of different data types. When the compiler allocates memory to these variables, it is allocated according to the order in which we define variables in the structure. Due to memory alignment, we do not use the bytes of some variables, which will cause a waste of memory, At this time, the purpose of saving memory space can be achieved by changing the order of member variables. Generally, it needs to be used in embedded development. For example, MCU has limited memory, but now MCU has enough memory, so there is basically no need to consider too many things in this regard.
If you are interested, please read this article written by Daniel:
Original text: http://www.catb.org/esr/structure-packing.
By Eric S. Raymond
Chinese version: https://github.com/ludx/The-Lost-Art-of-C-Structure-Packing.