C Language Learning Note: 22_ structure

Source: Internet
Author: User

/* * 22. Structure. c * * Created on:2015 July 9 * author:zhong * * #include <stdio.h> #include <stdlib.h>/** * structure : Encapsulation of common data * The structure is a bit of object-oriented thinking, the commonality of a class of things to be closed for use. * * Define struct: *//1> defines struct, can also be defined in function (global and local struct) struct person{char *name;//string int age; double hiegth;}; There is a semicolon at the end of 2>: Defines a struct and creates a struct variable struct person {char *name;//string int age;double hiegth;} p;3>: struct with no name, only once struct { Char *name; string int age;double hiegth;} P * *///---------structure of the basic definition and use-----------------------------------//define the structure, you can also define in the function (global and local structure) struct person {char *name ; string int age;double hiegth;};/ /There is a semicolon ending void use_struct () {//Create constructor variable struct person p = {"Zhong", 18, 159.0};//Create a constructor and assign an initial value (you can use {} assignment only at creation time) P.age = 19;p.hieg th = 170.0; You can use the variable name. The font name of the struct refers to the data segment in the struct body, printf ("%d\n", sizeof (P.name)); 4 Pointers 4 bytes of printf ("%d\n", sizeof (P.age)); 4printf ("%d\n", sizeof (p.hiegth)); 8printf ("%d\n", sizeof (p));   Total size of the structure: 16 The sum of the fields (calculated and if only a large number of 4+4+8, not enough:)//such as: the structure of the most int age, Char C, the current field, their total bytes should be 4+1=5. Because it's not 4.The number of accompany, so fill enough 8, the size of the output structure of 8printf ("P p.age=%d,p.name=%s,p.hiegth=%f\n", P.age, P.name, p.hiegth);} Nested definition of----------struct------------------------/** * struct is a nested definition of a struct that contains another structure: * For example: use struct date to encapsulate date data * Use Person_ to encapsulate a People's information, people's information has a birthday, birthday is a date, you can use the structure of the definition of date to define *///using struct date to encapsulate the data struct date{int year;//int month;//month int day;//Date};// Use Person_ to encapsulate a person's information struct Person_{char *name;//name struct Date birthday;};/ /test void N_struct () {//define a variable for struct person_, and initialize the struct person_ p={"Zhong", {2015,7,9}};//Access data printf for struct body in struct (" P.birthday.year=%d\n ", P.birthday.year);p. birthday.year=2016; Modify the data printf ("p.birthday.year=%d\n", P.birthday.year);} ------struct array-----------------------------void Struct_array () {//defines a struct array and defines a normal array, which is the type name variable name [size] struct person persons[10]={//defines the struct array and assigns the value {"Zhong1", 18,174.0},{"Zhong2", 19,175.0},{"Zhong3", 20,176.0}};//reads a struct and copies it to the variable p, Modify the data in P, the array is not affected by the struct person p=persons[0];p. age=20;//modify the data in P, array is not affected int age=p.age;//read Data printf ("P p.age=%d, P.name=%s,p.hiegth=%f\n ", P.age, P.name, p.hiegth);//The data in the array is modified directly by referencing the field with the array subscript, so the persons[0].age=20;//int age=persons[0].age;//traversal output int i;for (i=0;i<3;i++) {struct Person P=persons[i];p rintf ("P p.age=%d,p.name=%s,p.hiegth=%f\n", P.age, P.name, p.hiegth);}} -------pointer to struct-----------------------void p_struct () {//defines a struct struct student{int id;int age;};/ /define a struct variable holds struct struct Student s={01,19};//defines a struct pointer (the struct Student as a whole, a type), and points to the struct variable sstruct Student *p=&s;/  /Use pointer to modify data, the following two ways can (*p). id=2; p->id=3;//uses pointers to access the structure printf ("s.id=%d,s.age=%d\n", (*p). ID, (*p). age);//can also be written this way printf ("s.id=%d,s.age=%d\n", p- >id,p->age);//p in front without adding *, when writing code with Eclipse, as long as P.age, will automatically become p->age//of course can also be written like this printf ("s.id=%d,s.age=%d\n", S.id, S.age);//p the front without adding *, when writing code with Eclipse, as long as p.age, it will automatically become P->age}int main () {//use_struct ();//struct_array ();//p_struct (); N_struct ();//int a=1;//int *p=&a;//printf ("%p\n", &a);//28ff44//printf ("%p\n", p);//28ff44return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C Language Learning Note: 22_ structure

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.