iOS Learning tour------Play structure

Source: Internet
Author: User
Tags float double

1. Global variables and local variables
Local variables:
Concept: defining function Internal variables
Definition Format: Variable type variable name;
Scope: Starts from the definition of the line to the end of the block of code
Life cycle: From code execution to what line of definition begins, until the end of the code where it resides
Feature: You cannot have a variable of the same name in the same code block
Different code blocks can have variables with the same name, and internal variables override externally defined variables
Global variables:
Concept: Defining variables outside the function
Definition: Variable type variable name = value;
Declaration: Variable type variable name;
Features: 1, can not be repeated definition, but can be repeated declaration
2, the local variable can be the same name as the global variable, within the scope of the local variable, then the local variable will overwrite the global variable
3, if there is no definition of only declarations, the system automatically defines it and initializes it to 0
Scope: Starts from the defined line until the end of the file

Life cycle: Starts from the start of the program until the program exits

#include <stdio.h>  int num;//Declaration int main (int argc, const char * argv[]) {        printf ("num =%d\n", num);        int num =;    printf ("num =%d\n", num);    {        num = +;        int num = ten;        printf ("num =%d\n", num);        num =;    }    printf ("num =%d\n", num);             return 0;}
2. Structural body
Constructed type: Consists of an existing data type
1. Arrays: A type that consists of multiple data of the same type
Features: Only one type of number can be stored
2. Structure: Never store a set of data that represents a particular meaning
It is the data encapsulation
function-to-function encapsulation
Benefit: Improve the readability of your code
Improve data ease of use
Improve Code maintainability

Define the structure body:
1. Define the structure type
struct struct type name {
Member type member name;
...
};//must be added with a semicolon
2. Define structure variables by struct type
struct struct type name struct body variable name;

#include <stdio.h>int main (int argc, const char * argv[]) {    //    int scores[] = {59,60};     int  Age  name char *  sex char  height double  weight double            /    int ages;    char sex;    double height;    double weight;    Char *name;    //    int ages[50];    Char sex[50];    //    ....  defines a human struct type    struct person{int age        ;        char sex;        Double height;        Double weight;        char *name;    };  int num;  int * pointer;  define struct-body variables struct person person    ;  Access struct member    person.age = ten;        Person.sex = ' M ';     Person.weight = 20.2;        Person.height = 1.2;        Person.name = "Xiao Ming";        printf ("age =%d,sex =%c,weight =%.2lf,height =%.2lf,name =%s\n", Person.age,person.sex,person.weight,person.height, Person.name);       return 0;}

3. Define multiple types of structures

#include <stdio.h>/* first Way 1, define struct type 2, define structure variable */void test () {struct person{int age;    Double height;        }; struct person p1;}        /* The second way to define the struct type is to define the structure variable */void test1 () {struct person{int age;    Double height;        } P1,P2 = {20,1.75};        P1 = (struct person) {10,1.2};        printf ("age =%d,height =%.2lf\n", p1.age,p1.height);    printf ("age =%d,height =%.2lf\n", p2.age,p2.height);    struct type definition is not a duplicate name//struct person{//int age;    Double height; } P3,P4 = {20,1.75};}        /* The Third way to define an anonymous struct type is to define the struct-body variable */int main (int argc, const char * argv[]) {struct{int age;    char sex;    } P1,P2 = {, ' M '};    P1.age = 10;    P1.sex = ' W ';    printf ("age =%d,sex =%c\n", p1.age,p1.sex);    P1 = p2;    printf ("age =%d,sex =%c\n", p1.age,p1.sex);    P1 = (struct) {ten, ' w '};        1, does not support the overall assignment//2, struct type can not be reused struct{int age;    char sex;    } P3,P4 = {, ' M '};   return 0;} 
4. struct-Body scope
Inside a function, a struct type scope is like a local variable
struct type defined outside the scope of a global variable
Scope: Starts from the defined line until the end of the file
Note: struct types cannot be declared.
The function of a struct type, except that it cannot be declared, is the same as a normal variable

#include <stdio.h>struct person{    int age;    char sex;}; int main (int argc, const char * argv[]) {      struct person p2;        P2.sex = ' s ';    P2.age = ten;        struct person{        int age;    };      struct person p = {Ten};    printf ("Age =%d\n", p.age);      {        struct person p = {$};        struct monkey{            char *name;}        ;        }        return 0;}


5. Structure array
struct array: array element is a struct-body array
Defining an array of struct bodies
struct type array name [number of elements];
Array element type array name [number of elements];

#include <stdio.h>//generally the struct type is defined in the outer struct of the function dog{    char *name;    int age;}; void Test () {    //    int nums[] = {1,2,3,4};    if the struct is not initialized, it is garbage    //The  first way: To define an array of structs and then initialize the    struct Dog dogs[5];    Dogs[0] = (struct Dog) {"Wang Choi", 1};    Dogs[1].age = 1;    Dogs[1].name = "rhubarb";  traverse struct array        for (int i = 0;i < 5;i++)    {        printf ("age =%d,name =%s\n", dogs[i].age,dogs[i].name); C17/>}}void test2 () {    ////  define struct array for initialization at the same time    ///  If there is no explicitly initialized struct, all members of this struct will be initialized to 0    struct Dog dogs[10] = {"Wang Choi", 1},{"rhubarb", 2},{"Fu", 3},{"small black", 4},{"small white", 5};  computes the number of elements of an array    int len = sizeof (dogs)/sizeof (struct Dog);        for (int i = 0;i < Len; i++)    {        printf ("age =%d,name =%s\n", dogs[i].age,dogs[i].name);    }    } int main (int argc, const char * argv[]) {  //    test ();    Tset2 ();    return 0;}

6. Structure and pointer
struct pointer: pointer to struct body
The data type that the pointer points to * pointer variable name;
struct type * pointer variable name;

#include <stdio.h>struct student{    char *name;//name    int no;//study number    double score;//score};int main (int argc, const char * argv[]) {       struct Student stu = {"Big wood", 60,59};   define struct-body pointer    struct Student *SP1;    SP1 = &stu;//  defines the same time as the initialization of the    struct Student *sp = &stu;//The  first way to access the members of the struct through pointers    (*SP). Score = 60;< c12/>//The  Second Way (emphasis)    sp->name = "small wood";    Sp->no =;    printf ("name =%s,no =%d,score =%.2lf\n", stu.name,stu.no,stu.score);          return 0;}

7. Structure nesting
There can be other types of struct members inside a struct
Note points for structure nesting:
1, structs can not nest themselves, can not have bytes of this type of members
2, the structure can nest their own type of pointer

#include <stdio.h>//defines a date structure of struct time{int hour;//HH int minute;//mm int second;//ss};struct date{    int year;    int month;    int day; struct time time;};    struct employee{int no;    Char name[20];//Date of entry//int year;//int month;//int day;    struct Date indate;//birthday/int birthyear;//int birthmonth;//int birthday;    struct Date birthday;//separation date//int goyear;//int gomonth;//int goday; struct Date outdate;};        void Test () {struct Employee emp = {1, ' big screen ', {2014,10,14,{12,12,12}},{1990,10,14},{2014,11,14}}; printf ("no =%d,name =%s, entry date =%d-%d-%d%d:%d:%d\n", Emp.no,emp.name,emp.indate.year,emp.indate.month,emp.inda    Te.day, Emp.indate.time.hour,emp.indate.time.minute,emp.indate.time.second);    }struct person{char *name;//struct person son; struct person *son;};/ * 1. There are 5 students in a class, three classes. Write 3 functions to achieve the following requirements: (1) Ask for the average score of each course, (2) Find out more than two failed students, and output their academic number and failure course results; (3) Find the students with average score of 85-90 in each class and output their number and name */int Main (int argc, const char * argv[]) {struct person father = {"Father"};        struct person son = {"Son"};        Father.son = &son;    printf ("Son name =%s\n", father.son->name); return 0;}
8. Enumeration
Enumeration: One by one lists out
Enumeration effect: Eliminate Magic numbers
Usage scenario: When something has only a few values, it is enumerated
Defining enumeration formats
Enum Enum type name {
element,//comma
...
};
Note the point:
1, enumeration type definition, all elements are shaping constants
2, the essence of enumeration type is plastic

#include <stdio.h>//defines the gender enumeration type enum sex{man, Woman, other};//int man = 10;//enumeration after definition, the same variable as the member is not defined void test (      {//In the code that appears in this special meaning number, we magic number/int sex = 0;    enum sex sex;    printf ("%d\n", man);    int man = 10;      printf ("%d\n", man);    Sex = 1;//It is not possible to assign a number to a variable of an enumeration type, so that the meaning of the enumeration does not exist//man = 1; }//can specify each element value in an enumeration enum season{Spring = 2, Summer, Autumn, Winter, sother = 9};void Printseason (enum Season Seaso            N) {switch (season) {case Spring: The value after//case must be consistent with the member in the enumeration, do not appear the magic number printf ("Spring \ n");        Break            Case summer:printf ("Summer \ n");        Break            Case autumn:printf ("Autumn \ n");        Break            Case winter:printf ("Winter \ n");        Break            Case sother:printf ("not the season on earth \ \");        Break    Default:break;    }}int Main (int argc, const char * argv[]) {printf ("%d,%d,%d\n", Man,woman,other); printf ("%d,%d,%d,%d\n ", spring,summer,autumn,winter);        Enum Season Season = Summer;        Printseason (season); return 0;}

9. typedef:
Alias an existing data type
Basic data types
int char float double
Structural body
Enumeration
Pointer
1. Pointers to normal variables
2, the structure of the pointer
3. Function pointers
Basic data types
Definition Format: TypeDef has a data type alias;

#include <stdio.h>void testbasetype () {typedef int int;       Int num = 10;        typedef INT Integer;        Integer a = 20;   printf ("%d\n", a);        }/* to struct type alias */void teststruct () {///First: Define struct type and then alias struct _person{int age for struct type;    Char *name;        };        typedef struct _PERSON person;    Person P1 = {10, "Little Red"};   printf ("age =%d,name =%s\n", p1.age,p1.name);        The second way, the structure type is defined at the same time as the struct type alias typedef struct _dog{int age;    Char *name;        } Dog;    Dog dog = {1, "rhubarb"};        The third Way: Define an anonymous struct type while giving the struct an alias typedef struct{INT age;    char * name;        } Cat;   Cat cat = {1, "Tom"};       }//give the piece up alias Void Testenum () {///1, first define the enumeration type and then give the alias enum _sex{Man, Woman, and other};        typedef enum _SEX Sex;            Sex sex = man;//2, defining an enumeration type with an alias for the enumeration type typedef enum _season{Spring, Summer} Season;    Season Season = Spring; 3. Define anonymous enumeration type and raise an alias at the same time typedef enum {Cong, Suan, Jiang} Tiaowei; Tiaowei Zuoliao = Cong;    Pointer of}//4, pointer type//4.1 basic data type void Testtypedefbasicpointer () {typedef int * INTPOINTERTYPE;        int a = 10;       Intpointertype p = &a;       *p = 20;    printf ("%d\n", a);        }//4.2 the pointer to struct type alias void Testtypedefstructpointer () {typedef struct {char *name;    int age;        } person;        typedef person * PERSONPOINTERTYPE;        Person p = {"Zhang San", 10};            Personpointertype pp = &p;        typedef struct _cat{char *name;        int age;        } Cat, * catpointertype;        Cat cat = {"Kitten", 1};        Catpointertype Catpointer = &cat;    printf ("name =%s,age =%d\n", catpointer->name,catpointer->age);        }//4.3 alias for enumeration type Testtypedefenumpointer () {//First define enum type enum _sex{man, Woman, and other};       typedef enum _SEX * SEXPOINTER;            Enum _sex Sex = man;      Sexpointer Sexpointer = &sex;  *sexpointer = Woman;    printf ("%d\n", sex); }//4.4 the function pointer to an int sum (int num1,int num2) {return num1 + num2;} int minus (int num1,int num2) {return num1 + num2;} An alias to the function pointer//typedef the return value type of the function pointed to by the pointer (* function pointer type alias) (the parameter list of the function being pointed to); void Testtypedefmethodpointer () {//Int (*sumpointer) (in       T num1,int num2) = sum;////int (*minuspointer) (int num1,int num2) = minus;        typedef int (*methodpointertype) (int num1,int num2);        Methodpointertype sumpointer = sum;        Methodpointertype minuspointer = minus;    int rs = Sumpointer (10,20);    printf ("rs =%d\n", RS);       }//understand void Testtypedefarraypointer () {char names[][10] = {"XTF", "ZBZ", "WF"};       char (*arraypoiner) [10];        Arraypoiner = names;        typedef char (*ARRAYPOINTERTYPE) [10];       Arraypointertype pointer = names;   printf ("%s\n", pointer[0]);    }int Main (int argc, const char * argv[]) {////testbasetype ();//Teststruct ();        Testtypedefbasicpointer (); TesttypedefsTructpointer ();        Testtypedefenumpointer ();        Testtypedefmethodpointer ();    Testtypedefarraypointer (); return 0;}




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

iOS Learning tour------Play 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.