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 to play with structures

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

Share

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

Editor to share with you how to use C language to play with the structure, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

First, the declaration and definition of the structure. Declaration of structure

A structure is a collection of values called member variables. Each member of a structure can be a different type of variable

When we are dealing with things with multiple different data types, we can use structures to organize them.

For example, if a book has different data types such as title, author, price, publication date, etc., we can create structures to contain different data types of the book.

The structure declaration is the main method to describe the structure combination, and the syntax format is:

Struct structure name

{

Structure member 1

Structure member 2

Structure member 3

...

}; / / semicolons cannot be lost

[note]

The structure member can be either any basic data type or another structure, which is equivalent to the nesting of the structure. (commonly known as nesting dolls)

For example:

Struct Book// describes the relevant attributes of a book, where Book is the name of the framework {char name [20]; / / title char author [20]; / / author float price;// price}; / / semicolon must not be lost

This is equivalent to describing the framework of a book.

two。 Type of structure member

The types of structure members can be scalars, arrays, pointers, or even other structures.

3. Definition of structure

The declaration of the structure is only a simple description; in fact, it does not allocate space in memory until the structure type variable is defined.

In other words, it is not really used, it exists virtually, and only when a structure type variable is defined can it really exist.

For example, it defines the framework of the book.

Struct Book// describes the relevant attributes of a book, where Book is the name of the framework {char name [20]; / / title char author [20]; / / author float price;// price}; / / semicolon must not be lost

Here, in the compiler, memory space is not allocated, it is just a virtual existence. Once we have defined the structure type variable, it can be allocated space.

For example:

Struct Book// describes the relevant attributes of a book, where Book is the name of the framework {char name [20]; / title char author [20]; / / author float price;// price}; / / the semicolon must not lose the int main () {struct Book book;// local variable-put in the stack area return 0;}

We can also notice in the above example that the syntax for defining structural body variables is:

Struct structure name structure variable name

In addition, you can define structure variables when the structure is declared

Struct Book// describes the related attributes of a book {char name [20]; char author [20]; float price;} b1 direction b2 hand hand b2 is a global variable. Put it in the static area int main () {struct Book book;// local variable-put it in the stack area return 0;}

The b1 and b2 structure variables are global variables that can also be accessed in other functions.

Initializing the structure

We can initialize a variable or array when we define it.

For example:

Int adept 10 setint arr [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}

Similarly, when we define a structure variable, we can initialize it at the same time.

Struct Book// describes the related attributes of a book {char name [20]; char author [20]; float price;} b1 direction b2 hand hand b2 is a global variable. Put it in the static area int main () {struct Book book= {"The Smiling、Proud Wanderer", "Jin Yong", 30}; / / in this case, initialize the structure variable, that is, define the variable and assign the initial value return 0;} 3. Visit the structure member.

Structure variables access members of member structure variables are accessed through the dot operator (.). The dot operator accepts two operands.

For example, book.name is a name member that references a book structure variable, which is an array of characters.

# include struct Book// describes the related attributes of a book: {char name [20]; char author [20]; float price;} b1direction b2; / / b1Powerb2 is a global variable. Put it in the static area int main () {struct Book book= {"The Smiling、Proud Wanderer", "Jin Yong", 30}; / / in this case, initialize the structure variable, that is, define the variable and assign the initial value printf ("% s% f\ n", book.name, book.author, book.price); / / use. To access return 0;}

IV. Structural nesting

If you access nested structure members, you need to use multi-layer dot operators to operate. Because the structure of C language can only access the lowest-level members, if there is a multi-level structure nesting, it needs to go deep level by level until the lowest-level members are found.

Struct S {int a; char c; double d;}; struct T {struct S / structurally nested char name [20]; int num;}; int main () {struct T t = {{100 recording cantilever 3.14}, "Reese", 30} Printf ("% d% c% f% s% d\ n", t.s.a, t.s.c, t.s.d, t.name, t.num); / / used the two-layer dot operator to find the member return 0;}

Fifth, structural pointer

As said at the beginning, the members of a structure can be scalars, arrays, pointers.

Here, let's take a look at the structure pointer.

Struct Book * pt

Here you declare a pointer variable pt that points to the Book structure type

Struct S {int a; char c; double d;}; struct T {struct S s; char name [20]; int num;}; int main () {struct T t = {{100th) Printf ("% d% c% f% s% d\ n", t.s.a, t.s.c, t.s.d, t.name, t.num); struct T*pt = & t / / the way to get the address printf ("% d% c% f% s% d\ n", (* pt) .s.a, (* pt) .s.c, (* pt) .s.d, (* pt) .name, (* pt) .num); printf ("% d% c% f% s% d\ n", pt- > s.ptr. }

[note] the array name points to the address of the first element, so you can assign the array name directly to the pointer variable. However, the variable name of the structure variable does not point to the address of the structure, so you need to use the take address operator (&) to get its address.

As above:

Struct T*pt = & t handle / how to get the address

From the above example, we can also find that there are two ways to access structure members through structure pointers:

(1) (* structure pointer). Member name

(2) structure pointer-> member name

The first is due to the dot operator (.) It takes precedence over the pointer's value operator (*), so use a small slogan to dereference the pointer, make it the structure variable, and then use the dot operator to access its members.

The above two methods are completely equivalent in implementation. But remember, the period (.) Can only be used for structures, and arrows (- >) can only be used for structure pointers.

[the print result is the same]

When both are available, the second method is preferred because the arrowhead is directional and can be intuitively associated with the pointer.

VI. Structural transmission of parameters

When a function is called, the passing of parameters is the process of passing values, that is, the process of passing arguments to formal parameters. Therefore, the structure variable can be passed as the parameter of the function, and two structure variables of the same structure type also support direct assignment.

Struct S {int arr; int num; char ch; double d;}; / / structure pass parameter void print1 (struct S ss) {printf ("% d% c", ss.arr [0], ss.arr [2], ss.num,ss.ch,ss.d) } / / structure address pass parameters void print2 (struct S*ps) {printf ("% d% d% c", ps- > arr [0], ps- > arr [2], ps- > num, ps- > ch, ps- > d);} int main () {struct S s = {{1m 2pm 3PM 4PM 5}, 100,100, 'wrestling 3.14}; print1 (s); / / transmission structure print2 (& s) / / send address return 0;}

As you can see, the parameters are indeed passed.

So, which of the above print1 or print2 functions is better?

The answer is: the print2 function is preferred. Reason:

When passing parameters to a function, the parameters need to be stacked. If you pass a structure object, the structure is too large, the parameter stack of the system overhead is relatively large, so it will lead to performance degradation.

Therefore, when the structure passes parameters, it should pass the address of the structure.

The above is all the content of the article "how to play with the structure in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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