Pointer to struct

Source: Internet
Author: User

Pointer to struct

Outputs student information using pointer pointing to struct Array

Note: If the Pointer Points to the struct array, the starting address of the struct array is obtained. This address can be used to access all member variables in the struct array. The arithmetic operation of pointer to struct is similar to that of pointer to array.

# Include
 
  
# Define N 10 // struct type and variable definition, initialize struct student {char * number; char * name; char sex; int age; float score ;} stu [3] = {06001, Wang Chong, 'M', 22, 78.5}, {06002, Li Hua, 'F', 21, 87.0}, {06003, zhang Yang, 'M', 22, 90.0 }}; void main () {struct student * p; printf (basic student information table :); printf (Name: Gender and Age score ); for (p = stu; p
  
   
Number, p-> name, p-> sex, p-> age, p-> score );}
  
 

 

The running result is as follows;

In the above program, a pointer Variable p pointing to the struct is defined first. In the loop body, the Pointer Points to the struct array p = stu, that is, the Pointer Points to the starting address of the struct variable. Access members through p-> number, p-> name, and so on. P + 1 indicates the starting address of the second element stu [1] In the array. P + 2 indicates the starting address of the third element in the array, if:

During struct definition, the keyword typedef is usually used to create an alias for the defined data type. For example:

Typedef struct student StuInfo;

The alias of student Of the struct type is defined as StuInfo, so that StuInfo becomes a struct type. StuInfo can be used to define struct variables and pointer variables, for example:

StuInfo stu [3], * p;

You can also use typedef to create an alias for the basic data type, for example:

Typedef int DataType; // The DataType is equivalent to the int type.

You can use the keyword typedef to make the program more portable.

 

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.