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 refer to the structure pointer in C language

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to reference C language structure pointer". In daily operation, I believe many people have doubts about how to reference C language structure pointer. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how to quote C language structure pointer"! Next, please follow the editor to study!

Structure pointers, which can be subdivided into pointers to structure variables and pointers to structure arrays.

A pointer to a structural variable

Earlier, we used the "structure variable name." the member name refers to the members in the structure variable, in addition to this method, you can also use pointers.

As mentioned earlier, & student1 represents the first address of the structure variable student1, that is, the address of the first item of student1. If you define a pointer variable p to point to this address, p can point to any member of the structure variable student1.

So, what type is this pointer variable defined? Can only be defined as a structure type, and point to what structure type of structure variable, what kind of structure type should be defined. For example, if you point to a structure variable of type struct STUDENT, then the pointer variable must be defined as a struct STUDENT* type.

Let's modify the previous program in the form of a pointer:

# include # include struct AGE {int year; int month; int day;}; struct STUDENT {char name [20]; / / name int num; / / Student ID struct AGE birthday; / / Birthday float score; / / score}; int main (void) {struct STUDENT student1; / * define the structure variable student1*/ struct STUDENT * p = NULL with the struct STUDENT structure type / * define a pointer variable pause / p = & student1; / * p pointing to the structure variable student1, that is, the address of the first member * / strcpy ((* p). Name, "Xiao Ming"); / (* p) .name is equivalent to student1.name (* p). Birthday.year = 1989; (* p). Birthday.month = 3; (* p). Birthday.day = 29 (* p) .num = 1207041; (* p) .score = 100; printf ("name:% s\ n", (* p) .name); / / (* p) .name cannot be written as p printf ("birthday:% d-%d-%d\ n", (* p) .birthday.year, (* p) .birthday.month, (* p) .day.day); printf ("num:% d\ n", (* p) .num) Printf ("score:% .1f\ n", (* p) .score); return 0;}

The output is as follows:

Name: Xiao Ming

Birthday: 1989-3-29

Num: 1207041

Score: 100.0

As we can see, the way to refer to a member of a structure variable with a pointer is:

(* pointer variable name). Member name

Note that the parentheses on both sides of * p cannot be omitted because the member operator "." Takes precedence over the pointer operator "*", so if parentheses on both sides of * p are omitted, then * p.num is equivalent to * (p.num).

It can also be seen from the program that because the pointer variable p points to the address of the first member of the structure variable student1, that is, the first address of the character array name, p and (* p) .name are equivalent.

However, "equivalence" simply means that they represent the address of the same memory unit, but their types are different. The pointer variable p is of type struct STUDENT*, while (* p) .name is of type char*. Therefore, (* p) .name cannot be changed to p in strcpy. When using% s for input or output, input or output parameters can only be written as (* p) .name, not p.

Similarly, although & student1 and student1.name represent the address of the same memory unit, their types are different. & student1 is struct STUDENT* and student1.name is char*, so when initializing p, "paired student 1;" cannot be written as "p=student1.name". Because p is of type struct STUDENT*, you cannot assign a student1.name of type char* to p.

In addition, for convenience and intuition, use pointers to refer to members of structure variables:

(* pointer variable name). Member name

You can use it directly:

Pointer variable name-> member name

Instead, they are equivalent. "- >" is the pointing structure member operator, which has the same precedence as the structure member operator. " The same height. P-> num means that the pointer variable p points to the num member of the structure variable. P-> num ultimately represents the content of the member num.

Next, modify the program with "- >":

# include # include struct AGE {int year; int month; int day;}; struct STUDENT {char name [20]; / / name int num; / / student number struct AGE birthday; / * define the structural body variable birthday with struct AGE structure type, birthday * / float score; / / score}; int main (void) {struct STUDENT student1 / * define the structural variable student1*/ struct STUDENT * p = NULL; / * using the struct STUDENT structure type to define the pointer variable of the struct STUDENT structural type, paddress / p = & student1; / * p points to the first address of the structural variable student1, that is, the address of the first item * / strcpy (p-> name, "Xiao Ming"); p-> birthday.year = 1989; p-> birthday.month = 3; p-> birthday.day = 29 P-> num = 1207041; p-> score = 100; printf ("name:% s\ n", p-> name); / / p-> name cannot be written as p printf ("birthday:% d-%d-%d\ n", p-> birthday.year, p-> birthday.month, p-> birthday.day); printf ("num:% d\ n", p-> num); printf ("score:% .1f\ n", p-> score); return 0;}

The output is as follows:

Name: Xiao Ming

Birthday: 1989-3-29

Num: 1207041

Score: 100.0

However, it should be noted that "- >" can only be added after the "pointer variable name". Never add "- >" after the member name such as birthday.

To sum up, the following three forms are equivalent:

Structural body variable. Member name.

(* pointer variable). Member name.

Pointer variable-> member name.

The third of these methods is important and is usually used, while the other two are rarely used. The third way is also used when talking about the linked list later.

A pointer to an array of structures

When talking about numeric arrays earlier, you can assign the array name to a pointer variable so that the pointer variable points to the first address of the array, and then uses the pointer to access the elements of the array. Structure arrays are also arrays, so you can do the same.

We know that each element of the struct array is a struct variable. If you define a struct pointer variable and assign the array name of the struct array to the pointer variable, it means that the first element of the struct array, that is, the address of the first struct variable, is assigned to the pointer variable. For example:

# include struct STU {char name [20]; int age; char sex; char num [20];}; int main (void) {struct STU stu [5] = {{"Xiao Hong", 22, 'Fang, "Z1207031"}, {"Xiaoming", 21,' M é, "Z1207035"}, {"Xiao Qi", 23, 'Fitch, "Z1207022"}; struct STU * p = stu; return 0;}

At this point, the pointer variable p points to the first element of the structure array, which points to stu [0]. We know that when a pointer points to an array, the pointer can point to other elements of the array by moving.

This principle applies equally to struct arrays and struct pointers, so pendant 1 points to the first address of stu [1]; pendant 2 points to the first address of stu [2]. So as long as you use the for loop, the pointer can point to the elements of the structure array one by one.

It is also important to note that to assign a struct array name to a struct pointer variable, they must have the same struct type.

Let's write a program:

# include struct STU {char name [20]; int age; char sex; char num [20];}; int main (void) {struct STU stu [3] = {{"Xiao Hong", 22, 'Fang, "Z1207031"}, {"Xiaoming", 21,' M é, "Z1207035"}, {"Xiao Qi", 23, 'Fitch, "Z1207022"}; struct STU * p = stu; for ( Pname, p-> age, p-> sex, p-> num);} return 0;}

The output is as follows:

Name: Xiao Hong; age:22; sex:F; num:Z1207031

Name: Xiao Ming; age:21; sex:M; num:Z1207035

Name: Xiao Qi; age:23; sex:F; num:Z1207022

In addition, like the previous "relationship between ordinary arrays and pointers", when the pointer variable p points to stu [0], p [0] is equivalent to stu [0]; p [1] is equivalent to stu [1]; p [2] is equivalent to stu [2]. So stu [0] .num can be written as p [0] .num, the same goes for others. Let's modify the above program in p [I] way:

# include struct STU {char name [20]; int age; char sex; char num [20];}; int main (void) {struct STU stu [3] = {{"Xiao Hong", 22, 'Fang, "Z1207031"}, {"Xiaoming", 21,' M é, "Z1207035"}, {"Xiao Qi", 23, 'Fang, "Z1207022"}; struct STU * p = stu; int i = 0; for (; I)

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