Take notes (structure) on the tenth day of IOS stage, and take notes on the tenth day of ios stage.

Source: Internet
Author: User
Tags modulus

Take notes (structure) on the tenth day of IOS stage, and take notes on the tenth day of ios stage.

Knowledge Point sorting for IOS Learning (C language)

I. Data Structure

1) concept: data structure refers to the objects operated in a computer program-the relationships and operations between data and data elements.

2) The struct must be modified with the struct keyword.

Instance code:

1 struct Person {2 char name [20]; 3 int age; 4 float height; 5}; 6 7 int main () {8 struct Person Tom = {"Tom ", 22,180}; // struct Person is the data structure 9 // Tom is the variable name 10 Tom. age = 33; // access member variable 11 printf ("name = % s; age = % d; Height = %. 2f \ n ", Tom. name, Tom. age, Tom. height); 12 return 0; 13}

3) The difference between using typedef to modify a struct and without using typedef is that the struct modified by typedef can be used without adding struct.

Instance code:

1 typedef struct Person {2 char name [20]; 3 int age; 4 float height; 5} stPerson; 6 7 int main () {8 stPerson Jack = {"Jack ", 36,178}; // struct stPerson is the data structure 9 // Jack is the variable name 10 Jack. age = 25; // access member variable 11 printf ("name = % s; age = % d; Height = %. 2f \ n ", Jack. name, Jack. age, Jack. height); 12 return 0; 13}

4) Use of struct Sets

Instance code:

1 int main () {2 // defines an array, which can contain the stPerson Data Type 3 stPerson stu [10] ={}; 4 stPerson * pstu; 5 printf ("% s", stu [0]. name); 6 for (int I = 0; I <10; I ++) {7 sprintf (stu [I]. name, "Jack % d", I); 8 stu [I]. age = 10 + I; 9 stu [I]. height = 170 + I; 10} 11 pstu = stu; 12 // (* pstu ). name is equivalent to pstu-> name; 13 // pstu + I points to the I-th element 14 // the pointer uses-> to access the member variable 15 int len = sizeof (stu) of the object pointing) /sizeof (stPerson); 16 for (int j = 0; j <len; j ++) {17 printf ("name = % s; age = % d; height = %. 2f \ n ", (* pstu ). name, (* pstu ). age, (* pstu ). height); 18 pstu ++; 19} 20 return 0; 21}

5) structure byte alignment # pragma pack (n) n can be 1, 2, 4, 8, 16; set the system alignment modulus. Compare the alignment modulus with the alignment modulus among all members of the structure.

Instance code:

1 typedef struct 2 {3 char ch [3]; // single-byte alignment 4} aa; 5 6 typedef struct 7 {8 char ch [3]; 9 short B; // 2-byte alignment 10} AB; 11 12 typedef struct13 {14 char ch [3]; 15 short B; 16 int c; // alignment 17} ac according to 4 bytes; 18 19 typedef struct20 {21 char ch [3]; 22 short B; 23 int c; 24 long l; // alignment by 8 bytes 25} ad; 26 27 typedef struct28 {29 char ch; 30 long l; 31} AE; 32 33 int main () 34 {35 printf ("sizeof (aa) = % lu \ n ", sizeof (aa); // 336 printf ("sizeof (AB) = % lu \ n", sizeof (AB); // 637 printf ("sizeof (ac) = % lu \ n ", sizeof (ac); // 1238 printf (" sizeof (ad) = % lu \ n ", sizeof (ad )); // 2439 printf ("sizeof (AE) = % lu \ n", sizeof (AE); // 1640 return 0; 41}

6) Enumeration concept: a set of named Integer constants. The keyword declared by enumeration is enum.

Note:

1. Use commas (,) to separate enumeration elements.

2. If the first element is not assigned a value, the default value is 0. Then, add 1 to the element.

3. enumeration is a collection of unsigned int type packages.

4. enumeration is a constant, similar to macro # define.

5. Enumeration names cannot be duplicated

Instance code:

1 enum weekday 2 {3 Monday, 4 Tuesday, 5 Wednesday = 10, // can be assigned manually, followed by this base + 1 6 Thurday, 7 Friday = 2, // The values after the values can be 8 Saturday, 9 Sunday10} smaller than the previous values; 11 12 int main () 13 {14 enum weekday = Saturday; // enum weekday data type, day variable name 15 printf ("day = % u, sizeof (day) = % lu \ n", day, sizeof (day); // The result is 6,416}

7) usage of typedef; a new name of the old type of typedef, and an alias for the Type

Instance code:

1 typedef unsigned long ul; 2 typedef int * intp; 3 int add (int a, int B) {4 return a + B; 5} 6 7 // get a new name for this function pointer: pfunc 8 typedef int (* pfunc) (int, int); 9 int main () 10 {11 ul number = 100; 12 int a = 20; 13 intp p = & a; 14 pfunc pf = add; 15 printf ("number = % lu \ n ", number); 16 printf ("* p = % d \ n", * p); 17 printf ("add = % d \ n", pf (10, 20 )); 18 return 0; 19}

8) structure consortium. Generally, the consortium has two elements, that is, the Consortium usually only has two elements. during initialization, the system only selects one of them with a large memory to assign values.

Instance code:

1 typedef struct Person/Person can be omitted, followed by simplified name 2 {3 char name [20]; // member variable 4 int age; 5 int height; 6 union {7 int stuid; 8 char workname [20]; 9}; // share the same memory segment and parse it as needed. 10 // The memory size is calculated by a large value. 11 // during initialization, the first parsing result is 12} stPerson; 13 14 int main () 15 {16 stPerson xiaoli = {"xiaoli", 18,180, 97}; 17 stPerson xiaozhu = {"xiaozhu", 26,160, 60}; 18 // when initializing, the last member variable cannot be assigned a value of 19 printf ("sizeof (stPerson) = % lu \ n", sizeof (stPerson); 20 printf ("name: % s, age: % d, height: % d, stuid: % d \ n ", xiaoli. name, xiaoli. age, xiaoli. height, xiaoli. stuid); 21 22 // assign 23 sprintf (xiaozhu. workname, "% s", "qianfeng"); 24 printf ("name: % s, age: % d, height: % d, workname: % s \ n ", xiaozhu. name, xiaozhu. age, xiaozhu. height, xiaozhu. workname); 25 return 0; 26}

 

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.