Details based on structure and pointer

  • 2020-04-01 23:35:32
  • OfStack

Structure-pointer variable: the description of a structure-pointer variable and the use of a pointer variable to point to a structure variable
The value in a structure-pointer variable is the first address of the structure variable to which it refers. The structure variable can be accessed through a structure pointer, as is the case with Pointers to array elements and function Pointers.
The general form of structure pointer variable description is:
Struct structure name * structure pointer variable name
For example: struct stu *pstu;  
The general form of access is:
Member name: (*pstu).num
Or as follows:
Structure pointer variable - > Member name   : pstu - > num
It should be noted that the parenthesis around (*pstu) is necessary because the member character ". "has a higher priority than" * ". If you write *pstu. Num without the parentheses, it is equivalent to *(pstu. Num), so the meaning is completely wrong.  
Structure variable. Member name
(* structure pointer variable). Member name
Structure pointer variable - > Member name
The three forms used to represent the members of a structure are completely equivalent.
Structured array pointer variables : a structure-pointer variable can point to an array of structures where the value of the structure-pointer variable is the first address of the entire array of structures. A structure-pointer variable can also point to an element of a structure-pointer array, where the value of the structure-pointer variable is the first address of the structure-array element.
If ps is a pointer to the structured array, ps also points to element 0 of the structured array, ps+1 to element 1, and ps+ I to element I. This is consistent with a normal array.

#include <stdio.h> 
int main(void)
{
    int i; 
    struct student 
    {
        long int num;
        int      age;
        char*    name;
    }st[3]={{198,19,"zhangsan"}, {199,18,"lisi"},{200,19,"wangwu"}};   
    struct student*   p; 
    p=st; 
    printf("/n/n    NO.    age    name   /n"); 
    for(  ;p<=st+2;p++)
        printf("  %ld    %d   %s/n",p->num,p->age,p->name);
    getch(); 
    printf("/n/n    NO.    age    name   /n"); 
    for( p=st ;p<=st+2;p++)
       printf("  %ld    %d   %s/n",(*p).num,(*p).age,(*p).name);
    getch();
    return 0;
}

Related articles: