A pointer to a variable represents the starting position in memory
A pointer to a variable in the struct represents the starting position of the struct variable in memory, and it can also point to an array of struct variables
To define a pointer to a struct-body variable:
// Suppose you already have a struct named student struct Student *pstruct// struct type * pointer name;
There are three ways to refer to members in a struct by pointers
Demo
1# include <stdio.h>2# include <stdlib.h>3 /*4 1. Referencing struct members using5 6 7 */8 9 Ten intMain () One { A - structStudent - { the Charcname[ -]; - intInumber; - CharCsex; - intIgrade; +}student={"Girl", .,'W',2}; - + structStudent *pstruct; APstruct = &student;//point to student struct variable atprintf"-----------The sudent ' s information----------\ n"); -printf"name:%s\n",(*pstruct). CName); -printf"number:%d\n",(*pstruct). Inumber); -printf"sex:%c\n",(*pstruct). Csex); -printf"grade:%d\n",(*pstruct). Igrade); -printf"============ reference struct members using the-so symbol ==============\n"); inprintf"name:%s\n",pstruct->cName); -printf"number:%d\n",pstruct->inumber); toprintf"sex:%c\n",pstruct->csex); +printf"igrade:%d\n",pstruct->igrade); - the return 0; *}
1.student.inumber
2. (*pstruct). Inumber
3.pstruct->inumber
The latter two are members of struct variables that are referenced by struct variable pointers, and the 2nd is the reason for the addition of parentheses on *pstruct because the operation priority of the "*pstruct" is to be elevated, because in the default case. The precedence of an operator is higher than the precedence of the * operator.
Personal preferred to refer to a member in a struct by using the point to operator
Note: The position of the declared struct can be placed outside the main function, or it can be placed in the main function
Some examples of exercises:
1# include <stdlib.h>2# include <string.h>3 4 structStudent5 {6 Charname[ -];7 intInumber;8 CharCsex;9 intIgrade;Ten }student; One A intMain () - { - structStudent *pstruct; thePstruct = &student; -strcpy (Pstruct->name,"Yao Yao"); -Pstruct->inumber = .; -pstruct->csex='W'; +Pstruct->igrade= the; - +printf"------------The student ' s information---------\ n"); Aprintf"name:%s\n",(*pstruct). Name); atprintf"number:%d\n",(*pstruct). Inumber); -printf"sex:%c\n",(*pstruct). Csex); -printf"grade:%d\n",(*pstruct). Igrade); - - - return 0; in}
1# include <stdio.h>2# include <stdlib.h>3 4 5 //Create a student body structure6 structStudent7 {8 Charname[ -];9 Charsex;Ten intscore; One Charlovep[ -]; A }student; - - the - intMain () - - { + structStudent *pstruct; -Pstruct = &student; +printf"input:====================\n"); Aprintf"Name:"); atscanf"%s",&pstruct->Name); -GetChar ();//a getchar is required to accept the space symbol when reading the string using%s so that the next input is not affected -printf"Sex:"); -scanf"%c",&pstruct->sex); -printf"Score:"); -scanf"%d",&pstruct->score); inprintf"Love People:"); -scanf"%s",&pstruct->LOVEP); toprintf"print:====================\n"); +printf"name:%s\nsex:%c\nscore:%d\nlovepeople:%s\n",pstruct->name,pstruct->sex,pstruct->score,pstruct->LOVEP); - the return 0; *}
Because it is relatively simple, so there is no comment, there are 1 points in the practice,
1. Master the two ways to manipulate structural variables with pointers
C language pointer to struct variable