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 create the struct structure of C language

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to create the struct structure of C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create the struct structure of C language.

I. the key points of this chapter

Create a structure

The Origin of typedef and structure

Anonymous structure

Structure size

Structure pointer

Other

Second, create a structure

Let's start with a simple structure creation.

This is a relatively standard structure.

Struct people {int age; int id; char address [10]; char sex [5];}; / / Don't lose the semicolon.

It is important to be careful not to lose the semicolon.

What about creating structures like this?

Struct phone {char brand [10]; / / Brand int price;// Price}; struct people {int age; int id; char address [10]; char sex [5]; struct phone;}

Obviously, one structure can be nested within another.

Without such a design, it's okay to do so.

Struct people {int age; int id; char address [10]; char sex [5]; char phone_brand [10]; int phone_price;}

But too many members in the structure is not conducive to our later maintenance, try to ask: suppose there are 1000 members, can you quickly find the members you need? When we have a segmented structure, we can locate and view it quickly.

? A structure can nest another structure, so can a structure nest itself?

Struct phone {char brand [10]; int price; struct phone;}

After doing so, the compiler will give you an error.

What is the reason?

Since the size of the structure is undefined, can you calculate the size of the structure? It's impossible!

Since the size is uncertain, how much space should the compiler open up for the variable when you use this structure to create a variable? All compilers put an end to this possibility at the beginning of their design.

To ask the question, can structures nest their own structure pointers?

Struct people {int age; int id; char address [10]; char sex [5]; struct people* son [2];}

The answer is: yes.

There is no question of how much space should be allocated, because struct people* is a pointer type, and its size is fixed, 4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine.

Third, the origin of typedef and structure.

Let's start with a piece of code.

What does struct people {int age; int id;} a Splash a stand for? Int main () {a.age = 20; printf ("% d\ n", a.age); return 0;}

Question: what does a stand for?

In fact, we can look at the problem like this.

Struct people {int age;int id;} a; int main () {a.age = 20; printf ("% d\ n", a.age); return 0;}

What about int b?

Int bsetstruct people {int age;int id;} a; int main () {a.age = 20; printf ("% d\ n", a.age); return 0;}

Obviously, struct people {int age;int id;} represents the structure type, just like the shaping type to create variables.

So a here is the variable created by the structure.

You can also see why the semicolon is retained at the end of the structure creation.

Let's look at another piece of code:

Typedef struct people {int age; int id;} a; int main () {a.age = 20; printf ("% d\ n", a.age); return 0;}

Can typedef,a be used as a variable created by a structure at this time?

Obviously not, at this point the compiler will report an error.

The manner of understanding is consistent with the above.

Typedef struct people {int age;int id;} a

Similar to

Typedef struct people {int age;int id;} a * typedef int b

The an at this time is struct people {int age;int id;}

The role of typedef is to rename the type struct people {int age;int id;} to a.

I don't know if you've ever seen such code.

Typedef struct people {int age; int id;} brecedence; int main () {an A1; b b1; c C1 = & A1; a1.age = 5; b1.age = 6; C1-> age = 10; printf ("d% d% d\ n", a1.age, b1.age, C1-> age); return 0;}

Do you know the result of the operation?

What are the b, an and c here?

I won't dwell on it here. An and b are both struct people {int age;int id;} structure types, and c is the struct people {int age;int id;} * structure pointer type.

Running result:

Then promote it again.

Typedef struct people {int age; int id;} b, a, c [20]; what do the b, a, c stand for here?

I look forward to seeing you solve this problem.

IV. Anonymous structure

This is an anonymous structure.

Struct {int age; int id;}; int main () {struct p1; p1.age = 10; printf ("% d\ n", p1.age); return 0;}

Can anonymous structures create structural variables in this way?

At this point, the compiler will report an error

Such anonymous structures can only define variables when the structure is created.

Like this.

Struct {int age; int id;} p1; int main () {p1.age = 10; printf ("% d\ n", p1.age); return 0;}

Let's take a look at this code.

Typedef struct {int age; int id;} people; int main () {people p1; p1.age = 10; printf ("% d\ n", p1.age); return 0;}

Here we rename the anonymous structure, that is, rename the structure type to people.

Then we can naturally use the people type to create p1. You can also create p2, p3, and so on.

Running result:

Is the following code legal?

/ / anonymous structure type struct {int a; char b; float c;} x; struct {int a; char b; float c;} a [20], * p; int main () {/ / based on the above code, is the following code legal? P = & x; return 0;}

Warning: the compiler will treat the above two declarations as two completely different types. So it's illegal.

5. Size of the structure

How to find the size of the structure type?

This requires understanding how structure members are stored in memory.

Do you know the running result of the following code?

Struct people {char a; int b; char c;}; int main () {struct people p1; printf ("% d\ n", sizeof (p1)); / / is the size 6? Return 0;}

Char is a byte size

Int is a four-byte size

Char is a byte size

The direct sum equals 6.

So is the size of this structure 6?

But we found that the answer was 12.

Why is that?

To put it simply, in order to improve efficiency and avoid reading errors when reading memory, the compiler does memory alignment.

What is memory alignment?

Is the alignment operation that allows the data to be arranged in the right place.

Why memory alignment?

I. reasons for transplantation

1. Not all hardware platforms can access any data at any address.

two。 Some hardware platforms can only fetch certain types of data at certain addresses, otherwise a hardware exception is thrown.

Second, performance reasons:

In order to access unaligned memory, the processor needs to make two memory accesses; aligned memory accesses require only one access.

Alignment rules:

The default alignment number in Windows is 8. The default alignment number in Linux is 4.

One: the first data member is stored at the offset address of 0.

Two: the number of alignment: equal to the default number of alignment and the smaller value with the size of the member.

Three: starting from the second member, starting from the offset address where it aligns with an integer multiple of the number.

Fourth, the size of the final structure needs to be adjusted to an integral multiple of the maximum alignment.

Maximum number of alignments: the maximum number of alignments of all members.

Struct people {char a; int b; char c;}

Parsing:

The first is char, which is placed directly at the offset address of 0.

The second is int, which has a size of 4, which is smaller than the default alignment, so its alignment is 4.

4 is an integral multiple of 4, so start putting data at the offset address of 4.

The third is char, which has a size of 1, which is smaller than the default alignment, and its alignment is 1.

8 is an integral multiple of 1, starting at the offset address of 8.

The total size is 9, not an integer multiple of the maximum alignment

To waste 3 spaces, adjusted to 12.

Let's take a look at the following code:

After swapping char and int member variables, is the structure still 12 in size?

Struct people {char a; char c; int b;}; int main () {struct people p1; printf ("% d\ n", sizeof (p1)); / / what is the size? Return 0;}

Parsing:

The first one: start directly from 0.

The second is char: the alignment number 1 is an integral multiple of 1, starting from the location where the offset address is 1.

The second is int: the alignment number is 4 and 4 is an integral multiple of 4, starting with the offset address of 4.

After placing each member variable, the total size is 8, which is an integral multiple of the maximum alignment (4) and does not need to be adjusted.

So the size of this structure is 8.

How about taking a look at the following?

Struct people {char a; int b; short c [2]; char d;}; int main () {struct people p1; printf ("% d\ n", sizeof (p1)); / / is the size 6? Return 0;}

Parsing:

The first member is char, which is placed directly at address 0.

The second member is int, with an alignment of 4, starting at the location where the offset address is 4.

The third member is short [2]. For the array, its alignment is the smaller value of the size of the first element and the default alignment, where it is 2, and then stores 4 bytes from the offset address of 8.

The fourth member is char, with an alignment of 1, which directly releases the 12-bit position.

The total size is 13, the maximum alignment is 4, not an integer multiple of the maximum alignment, need to be aligned to an integer multiple of the maximum alignment, waste 3 bytes, alignment to 16.

So the size of this structure is 16.

VI. Structural pointer

Create a structure first

Struct people {char a; int b;}

Then create a variable with the structure, and then use the structure pointer to point to the variable.

Int main () {struct people p1; struct people* p = & p1; return 0;}

The so-called structure pointer is the pointer to the structure.

Just like the shaping pointer, that is, the pointer to the shaping.

Access variable mode 1:

Int main () {struct people p1; struct people* p = & p1; p1.a = 'averse; p1.b = 10; printf ("% c% d\ n", p1.a, p1.b); return 0;}

Access variable mode 2:

Int main () {struct people p1; struct people* p = & p1; p-> a ='a; p-> b = 10; printf ("% c% d\ n", p-> a, p-> b); return 0;}

Access variable mode 3:

Int main () {struct people p1; struct people* p = & p1; (* p). A = 'await; (* p) .b = 10; printf ("% c% d\ n", (* p) .a, (* p) .b); return 0 } at this point, I believe you have a deeper understanding of "how to create the struct structure of C language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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