C language-06 complex data type-04 struct,-06-04

Source: Internet
Author: User

C language-06 complex data type-04 struct,-06-04
Structure Description (construction type)

Array: it can only contain multiple data of the same type.


Struct: It can be composed of multiple different types of data. The struct type does not exist and is defined by yourself.

Int main () {// 1. when defining the struct type definition type, there are three variables in struct Person {// without memory allocation, which can be called the struct member or attribute int age; // age double height; // height char * name; // name}; // note the semicolon // 1. define struct type struct Person p = {20, 1.55, "jack"}; p. age = 30; p. name = "rose"; printf ("age = % d, name = % s, height = % f \ n", p. age, p. name, p. height);/* the error code is struct Person p2; p2 = {30, 1.67, "jake"}; */struct Person p2 = {. height = 1.78 ,. name = "jim ",. age = 30}; // p2.age = 25; return 0 ;}

 

1. three methods for defining struct variables 1> define types first, then define variables (defined separately) struct Student {int age ;}; struct Student stu; 2> define both the type and the variable struct Student {int age;} stu; struct Student stu2; 3> define both the type and variable (the type name is omitted) // struct {int age;} stu cannot be reused;
Struct Array
 
Int main () {struct RankRecord {int no; // number 4 char * name; // name 8 int score; // points 4 }; /* struct RankRecord r1 = {1, "jack", 5000}; struct RankRecord r2 = {2, "jim", 500}; struct RankRecord r3 = {3, "jake", 300}; */struct RankRecord records [3] = {1, "jack", 5000}, {2, "jim", 500}, {3, "jake", 300 }}; for (int I = 0; I <3; I ++) {printf ("% d \ t % s \ t % d \ n", records [I]. no, records [I]. name, records [I]. score);} return 0 ;}
Pointer to struct
# Include <stdio. h>/* 1. definition of pointer to struct Student * p; 2. use pointers to access struct members 1> (* p ). member name 2> p-> member name */int main () {struct Student {int no; int age ;}; // struct variable struct Student stu = {1, 20}; // the pointer Variable p points to the struct Student * p in the future; // the pointer Variable p points to the stu Variable p = & stu; p-> age = 30; // The first method: printf ("age = % d, no = % d \ n", stu. age, stu. no); // method 2 printf ("age = % d, no = % d \ n", (* p ). age, (* p ). no); // The third method is commonly used printf ("age = % d, no = % d \ n", p-> age, p-> no ); return 0 ;}
Struct and Function
# Include <stdio. h> struct Student {int age; int no ;}; // If the struct is used as the function parameter, only the values of all the members of the real parameter struct are assigned to all the members of the form parameter struct. // modifying the internal struct of the function does not affect the void test (struct Student s) of the external real parameter struct) {s. age = 30; s. no = 2;} // it will affect the void test2 (struct Student * p) {p-> age = 15; p-> no = 2 ;} void test3 (struct Student * p) {struct Student stu2 = {15, 2}; p = & stu2; p-> age = 16; p-> no = 3 ;} int main () {struct Student stu = {28, 1}; // test (stu); // test2 (& stu); test3 (& stu ); printf ("age = % d, no = % d \ n", stu. age, stu. no); return 0 ;}
Nested definition of struct
# Include <stdio. h> int main () {struct Date {int year; int month; int day ;}; // type struct Student {int no; // Student ID struct Date birthday; // birthday struct Date ruxueDate; // admission Date // This is incorrect // struct Student stu ;}; struct Student stu = {1, {2000, 9, 10 },{ 2012, 9, 10 }}; printf ("year = % d, month = % d, day = % d \ n", stu. birthday. year, stu. birthday. month, stu. birthday. day); return 0 ;}
Enumeration
# Include <stdio. h> int main () {enum Sex {Man, Woman, Unkown}; // 0 male 1 female-1 unknown // int sex = 3; // enum Sex s = Unkown; // 1. define enum Season {spring = 1, summer, // The enumerated values are actually Integer constants starting from 0. You can also customize the start value autumn, winter}; // 2. defines the enum Season s = 100000; // the C language is a weak type language, and the incorrect enumerated value does not report printf ("% d \ n", s ); // return 0 is an integer constant ;}

 

Related Article

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.