C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (12)

Source: Internet
Author: User
Tags definition
Second, the structure of the pointer
A structure pointer is a pointer to a structure. It is defined by a "*" operator that is added to the structure variable name, such as defining a struct pointer with the previously described structure as follows:
struct string{
Char Name[8];
Char sex[2];
int age;
Char addr[40];
}*student;
You can also omit the structure pointer name only as a structure description, and then use the following statement to define the structure pointer.
struct string *student;
Access to struct members by using structure pointers differs from structural variables ' access to struct members in terms of expression. The structure pointer's access to a struct member is represented by:
Structure pointer name-> struct member
where "->" is a combination of two symbols "-" and ">", as if an arrow points to a struct member. For example, to assign a value to name and age in the structure defined above, you can use the following statement:
strcpy (Student->name, "Lu g.c");
student->age=18;
In fact, Student->name is the abbreviated form of (*student). Name.
You need to point out that the structure pointer is a pointer to the structure, the first address of a member in the structure, so you should initialize the structure pointer before use, that is, the byte space that allocates the entire length of the structure, which can be done with the following function, which is still described as follows:
student= (struct string*) malloc (Size of (struct string));
The size of (struct string) automatically takes the byte length of the string structure, and the malloc () function defines an area of memory that is the size of the structure and then returns its bluff address as a struct pointer.
Attention:
1. Structure as a data type, so the definition of structural variables or structural pointer variables also have local variables and whole variables, depending on the location of the definition.
2. The structure variable name is not the address that points to the structure, which is different from the meaning of the array name, so if you want to require that the first address of a member in the structure should be the &[structure variable name].
3. Complex forms of structures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.