[C language] structure pointer

Structure pointer

  • structure
    • basic knowledge
    • Pay attention to the assignment of members
  • structure pointer
    • pointer to structure variable
    • Struct pointers and struct member pointers
    • Referencing struct members with struct pointers

Structure

Basic knowledge

To get acquainted with the structure first, you can read this easy-to-understand article Structure-Basic
The so-called structure is a set of related variables (or arrays) whose type can be different.
The variables (or arrays) that make up a structure are called members of the structure.
C language requires that when defining a structure variable, it is necessary to clearly specify the name and type of each member .
To declare a structure type, you need to define a identifier, which is called a structure type identifier.

struct student
{<!-- -->
    char name[20];
    char num[11];
    int age;
    float score;
};//student is the identifier of the structure type
//struct student is a structure type

What operations can be performed on the members of the structure variable depends on the type of the member of the structure variable. Because the members of structure variables are also variables (or arrays) in essence, the operation of structure variable members is the same as Operations on variables (or arrays) of the same type are same.

Pay attention to the assignment of members

Let's first look at a wrong program:

#include <stdio.h>
struct student
{<!-- -->
char name[20];
char num[20];
int score;
};
int main (void)
{<!-- -->
struct student stu;
stu.score=99;
stu.name="Lin hui";
stu.num="S2208017";
printf("%d,%s,%s\
",stu.score,stu.name,stu.num);
return 0;
}

The compilation system will report an error, why is this?
When compiling this program, an error "assignment to the array name" will occur.
This is because the two members num and name of the structure variable stu are both character arrays.
And the array name belongs to the pointer constant, so the array name cannot be assigned.
So, how can string be stored as a character array member? We can use the strcpy function.
Correct procedure:

#include <stdio.h>
#include <string.h>
struct student
{<!-- -->
char name[20];
char num[20];
int score;
};
int main (void)
{<!-- -->
struct student stu;
stu.score=99;
strcpy(stu.name,"Linhui");
strcpy(stu.num,"S2208017");
printf("%d,%s,%s\
",stu.score,stu.name,stu.num);
return 0;
}

Structure pointer

The basic function of a pointer is indirect reference, that is, indirectly referencing another variable through a pointer variable.

Why use pointers?
The unique advantage of pointers is that some functions in C language can only be realized by means of indirect reference.
For example, inter-function rewriting of local variables can only be realized through indirect reference.

Similarly, the function of the struct pointer is to indirectly reference the structure variable.
However, some functions in C language (such as dynamic linked list) must be implemented with the help of structure pointer.

Pointer to structure variable

struct student //declare structure type
{<!-- -->
    char name[20];
    char num[20];
    int age;
    int score;
};
struct student stu;//Define the variable stu of the structure type
struct *p;//Define the pointer variable p pointing to the structure variable
p= & amp;stu;//assign initial value

Structure pointer and structure member pointer

Once the pointer to the structure variable is defined, the structure variable pointed to by can be referenced indirectly through the pointer.

p= & amp; stu;

Then, you can use *p to indirectly reference the structure variable stu;
The above is to assign the first address of the structure variable to the pointer variable p, so the structure pointer variable p points to the structure variable stu.
example:

Although the structure pointer has the same address value as the first member of the structure, the meanings of struct pointer and struct member pointer are different of.

struct student *p;*p=stu;
int *p1=stu.num;

Although the values of p and p1 are the same, both are 2000H. But p is a pointer to a variable of type struct, while p1 is a pointer to a variable of type int. If p ++ is executed, the value of p is 2043H, and p1 ++ is executed, the value of p1 is 2002H.

Reference structure members with structure pointer

Since structure pointer can be used to refer to structure variable, of course it can also refer to the member of the structure variable.
Its general form is:

(*structure pointer variable).member name

Note that the brackets are essential here. If after removing the parentheses:

*Structure pointer variable. Member name

Since the membership operator“.” has a higher priority than the indirect reference operator”*”, the above formula is:

*(structure pointer variable. member name)

And a pointer variable cannot have members, resulting in a syntax error.
example:
Input a student's student number, name, age and grade from the keyboard, and store it in a structure variable, and then output each data in sequence. It is required to use the structure pointer ``indirect reference to the member of the structure variable`.

#include <stdio.h>
struct student //define the structure type specifier student
{<!-- -->
    char num[20];
    char name[20];
    int age;
    float score;
};
int main (void)
{<!-- -->
    struct student stu,*p= &stu;
    gets((*p).num);//Enter student number
    gets((*p).name);//Enter the name
    scanf("%d", & amp;(*p).age);//Enter age
    scanf("%f", & amp;(*p).score);//Enter score
    printf("Student ID\tName\tAge\tGrade\
");
    printf("%s\t%s\t%d\t%.2f\
",(*p).num,(*p).name,(*p).age,(*p).score) ;
    return 0;
}

The above indirect reference form is more intuitive, but a bit cumbersome, so C language provides another more concise form of reference.
Its general form is:

Structure pointer variable -> member name

The "->" here is called the points to operator.
It is functionally equivalent to:

(*structure pointer variable).member name

Let's do the above problem again in this way:
Input a student's student number, name, age and grade from the keyboard, and store it in a structure variable, and then output each data in sequence. It is required to use the structure pointer ``indirect reference to the member of the structure variable`.

#include <stdio.h>
struct student //define the structure type specifier
{<!-- -->
    char num[20];
    char name[20];
    int age;
    float score;
};
int main (void)
{<!-- -->
    struct student stu,*p= &stu;
    gets(p->num);
    gets(p->name);
    scanf("%d", &p->age);
    scanf("%f", &p->score);
    printf("Student ID\tName\tAge\tGrade\
");
    printf("%s\t%s\t%d\t%.2f\
",p->num,p->name,p->age,p->score);
    return 0;
}
syntaxbug.com © 2021 All Rights Reserved.