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 use C language structure

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "how to use the C language structure". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the C language structure" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

Essence

A collection of values.

Simple use of # include struct User {char uName [20]; char uPass [20];}; int main () {struct User U1 = {"abc", "123"}; printf ("% s\ n", u1.uName); printf ("% s\ n", u1.uPass); return 0;}

Effect:

Some writing methods

Immediately after the declaration, you can create an instance.

You can also declare it separately.

# include struct User {char uName [20]; char uPass [20];} U1, U2, u3terstruct User u4itint main () {struct User u5; return 0;}

Anonymous structure: I used it, and you'll never use it again.

Struct {char uName [20]; char uPass [20];} x; I set myself up

Can only cover the pointer, because the space is limited.

Example: linked list.

# include struct Node {int val; struct Node* next;}; int main () {struct Node a = {333 last null}; struct Node b = {666 last null}; printf ("% d\ n", (* b.next) .val); return 0;}

Effect:

Memory alignment

The structure puts the members, one by one.

Put the first one at the first address.

The second starts by calculating an alignment number, which is the lesser of the following two values:

1the default value of "1" vs "8".

2, the number of bytes occupied by the member.

The actual offset is an integral multiple of the alignment.

Finally, the size of the structure is an integral multiple of the maximum alignment.

For example-int char char#include struct A {int a; char b; char c;}; int main () {struct An a = {1 lld, sizeof (a); return 0;}

Effect: 8

Illustration:

Int comes first.

Char occupies 1 byte, so put it directly.

So is the second char.

The final length is an integral multiple of the number of bytes in int, that is, 8.

For example-char int char#include struct A {char b; int a; char c;}; int main () {struct An a = {1 lld, sizeof (a); return 0;}

Effect: 12

Illustration:

Play char directly.

The int starts with the fourth one.

Play char directly.

Final length: a multiple of the first 4 starting at 9, 12.

For example-char char int#include struct A {char b; char c; int a;}; int main () {struct An a = {1 lld, sizeof (a); return 0;}

Effect: 8

Illustration: two char are put directly.

Int starts with the next integer multiple of 4, that is, the fifth.

Final length: a multiple of the first 4 starting at 8, 8.

Access to members by structure pointers

It's a way of writing arrowheads.

# include struct A {int a;}; int main () {struct An a = {333}; struct A * b = & a; printf ("% d\ n", b-> a); return 0;}

Effect:

After reading this, the article "how to use the C language structure" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, welcome 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