Detailed C language structure pointer

Structure pointer, may be subdivided into pointers and pointers to point to an array of structures of variable structure.

Pointer to a variable structure

We refer to members of the front structure by a variable "variable name structure. Member name" approach, in addition to this method can also use the pointer.

Recall, & student1 represents a structure variable student1 the first address, i.e. the address of the first entry student1. If the definition of a pointer variable p to point to this address, then, p can be any point in the structure of a variable student1 member.

Then, the pointer variable is defined as what type it? Can be defined as a structure type and structure type of pointing what structure variables, it is necessary to define what type of structure. Struct STUDENT such point type structure variables, the pointer variables must be defined struct STUDENT * type.

Will modify the foregoing procedure pointer manner:

The include # <stdio.h> 
# the include <string.h> 
struct of AGE 
{ 
    int year; 
    int month The; 
    int Day; 
}; 
struct the STUDENT 
{ 
    char name [20 is]; // name 
    int num; // Science No. 
    struct AGE birthday ; // birthday 
    float score; // score 
}; 
int main (void) 
{ 
    struct the STUDENT student1; / * struct variable with the definition of the structure type of the STUDENT structure * student1 / 
    struct the STUDENT * P = NULL; / * define a point struct STUDENT structure type of pointer variable p * / 
    p = & student1; / * first address p points to the structure variable student1, i.e., address of the first member * / 
    strcpy ((* p) .name, "Bob"); / /(*p).name equivalent to student1.name 
    (* P) = 1989 .birthday.year;  
    (* P) =. 3 .birthday.month;
    (* P) .birthday.day = 29;
    (*p).num = 1207041;
    (*p).score = 100;
    printf("name : %s\n", (*p).name);  //(*p).name不能写成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:
name: Xiao Ming
Birthday: 1989-3-29
NUM: 1207041
Score: 100.0

, we see the way the members of variable structure with a pointer reference is:

(* Pointer variable name). Member Name

Note that, on both sides of the brackets * p can not be omitted, because the members of the operator. "" Precedence over the pointer operator "*", so if the parentheses around omitted * p, then * * p.num is equivalent to ( p.num) a.

The program can also be seen: because the pointer variable p points to the address of the first member student1 structure variable, i.e., the first address name character array, so p and (* p) .name are equivalent.

However, the "equivalence" is just to say they represent the same address of a memory cell, but their type is different. P is a pointer variable of type struct STUDENT *, while (* p) .name is char * type. So strcpy can not be (* p) .name changed to p. When the input or output% s, parameter input or output parameters can only be written (* p) .name not written p.

Also, the same address of the memory cell while student1.name & student1 and represented, but they are different types. & student1 type is struct STUDENT *, char * is the student1.name type, so when p is initialized, "p = & student1;" can not be written "p = student1.name". Because p is a struct STUDENT * type, it can not be assigned to char * type student1.name p.

In addition for convenience and intuitive to use, with a pointer reference variable structure members:

(* Pointer variable name). Member Name

It can be used directly:

Pointer variable name -> Member Name

Instead, they are equivalent. "->" is the "structure members point operator", the priority of the same structure as high member operator. "." Meaning p-> num are: num variable structure member pointer variable p points in. p-> num num is the content of the final behalf of the members of.

Then the following procedures "->" changed a bit:

The include # <stdio.h> 
# the include <string.h> 
struct of AGE 
{ 
    int year; 
    int month The; 
    int Day; 
}; 
struct the STUDENT 
{ 
    char name [20 is]; // name 
    int num; // Science No. 
    struct AGE birthday ; / * struct AGE structure is defined by the type of structure variables birthday, birth * / 
    a float score; // score 
}; 
int main (void) 
{ 
    struct STUDENT student1; / * struct STUDENT structure is defined by the type of structure variables student1 * / 
    struct STUDENT * P = NULL; / * struct STUDENT defined structure type of pointer variable * P / 
    P = & student1; / * structure variable student1 point P of the first address, i.e. the address of the first item * / 
    strcpy (p- > name, "Bob"); 
    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不能写成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:
name: Xiao Ming
birthday: 1989-3-29
NUM: 1207041
Score: 100.0

But note that only the "pointer variable name" back to add "->", do not add the name of a member in the back as birthday "->."
In summary, the following three forms are equivalent:

  • Structure variables. Member name.
  • (* Pointer variable). Member Name.
  • Pointer Variables -> member name.


The first of three ways is important, often use this approach, two other ways used much. Back when talking about the list are also 3 ways to use.

Pointer to an array of structures

Speaks in front of the array when the array of numeric name can be assigned to a pointer variable, so that the pointer points to the first address of the array variable, then element pointer array accesses. Array of structures is an array, so you can do the same.

We know that each element of the array of structures is a structure variable. If you define a structure pointer variable and the array name array of structures assigned to the pointer variable, then it means that the first element of the array of structures, that is, the first address of a structure variable, ie first address of the first member of structure variables assigned to the pointer variable. such as:

# include <stdio.h>
struct STU
{
    char name[20];
    int age;
    char sex;
    char num[20];
};
int main(void)
{
    struct STU stu[5] = {{"小红", 22, 'F', "Z1207031"}, {"小明", 21, 'M', "Z1207035"}, {"小七", 23, 'F', "Z1207022"}};
    struct STU *p = stu;
    return 0;
}

The pointer variable p to point to the first element of the array of structures, i.e., point stu [0]. We know that when a pointer to an array of pointers can point to other elements of the array by moving way.

This principle also applies to an array of structures and the structure pointer, the p + 1 points to stu [1] of the first address; p + 2 points to stu [2] ...... so long as the first address for use cycle, the pointer can one directed structural array elements.

Also note that, to an array of structures were assigned to a structure pointer variable, then they must be the same type of structure.

Write the following program:

# include <stdio.h>
struct STU
{
    char name[20];
    int age;
    char sex;
    char num[20];
};
int main(void)
{
    struct STU stu[3] = {{"小红", 22, 'F', "Z1207031"}, {"小明", 21, 'M', "Z1207035"}, {"小七", 23, 'F', "Z1207022"}};
    struct STU *p = stu;
    for (; p<stu+3; ++p)
    {
        printf("name:%s; age:%d; sex:%c; num:%s\n", p->name, p->age, p->sex, p->num);
    }
    return 0;
}

The output is:
name: red; Age: 22 is; Sex: F.; NUM: Z1207031
name: Bob; Age: 21 is; Sex: M; NUM: Z1207035
name: small seven; age: 23; sex: F ; num: Z1207022

addition with "normal relationship between arrays and pointers" as before, when the pointer variable p to point stu [0] when, 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, empathy other. Will modify the above procedures p [i] by:

# include <stdio.h>
struct STU
{
    char name[20];
    int age;
    char sex;
    char num[20];
};
int main(void)
{
    struct STU stu[3] = {{"小红", 22, 'F', "Z1207031"}, {"小明", 21, 'M', "Z1207035"}, {"小七", 23, 'F', "Z1207022"}};
    struct STU *p = stu;
    int i = 0;
    for (; i<3; ++i)
    {
        printf("name:%s; age:%d; sex:%c; num:%s\n", p[i].name, p[i].age, p[i].sex, p[i].num);
    }
    return 0;
}

The output is:
name: red; Age: 22 is; Sex: F.; NUM: Z1207031
name: Bob; Age: 21 is; Sex: M; NUM: Z1207035
name: small seven; age: 23; sex: F ; num: Z1207022

Source: http://c.biancheng.net/view/246.html

Guess you like

Origin www.cnblogs.com/llanse-xianchengduo/p/10960498.html