IOS getting started tutorial (III)-C Language Features

Source: Internet
Author: User

IOS getting started tutorial (III)-C Language Features

// 3. C language features // 3.1 Function Definition // function return type function name (parameter) {// dosomthing //} This is the general function definition, // there is an old function definition method. The parameter void printMesg (msg, loopNum) int loopNum; int msg; {int I; for (I = 0; I
 
  
23 dosomething # elif AGE> 20 doOtherThing # esle doSomeThing # endif * // 3.10 struct type name {// member list} struct rect {int x; int y; int width; int height;}; struct rect rect1; // It is too troublesome to add struct every time # define RECT struct rect // 3.10typedef statement, so be careful when using it! // The usage of these two conditions can bring about the benefits of struct point {int x; int y ;}; typedef struct point YLPoint; enum season {spring, summer, fall, winter }; typedef enum season YLSeason; // then you can use them to define struct variables and enumeration variables for YLPoint p1; YLSeason s1; // 3.11 initialize the struct variable // You can initialize the struct variable directly // This is the correct struct rect {int x; int y; int width; int height ;} rect1 = {20, 30, 100,200}; // This is the error rect1 = {12, 20, 20, 90}; struct point {int x; int y ;}; typedef poi Nt YLPoint; // The correct YLPoint p1 = {20, 10}; // The incorrect p1 = {10, 10}; // The correct p1.x = 10; p1.y = 10; // 3.12 struct array YLPoint points [] = }}; // The following code is incorrect: points [0] = {}; // correct method: points [0]. x = 20; points [0]. y = 30; // 3.13/* 1. define block // define block void (^ printString) (void) = ^ (void) {NSLog (@ "block without return value ");}; // define a block with parameters and return values. Note that only the parameter type is required in the block, and the parameter int (^ addInt) (int, int) is not required) = ^ (int int_a, int I Nt_ B) {return int_a + int_ B;}; // The block can access the value of the local variable but cannot change int myInt = 20; void (^ printInt) (void) = ^ (void) {myInt = 30; NSLog (@ "myInt Value % d", myInt) ;}; myInt = 80; printInt (); // The printed result is 20. Note: When the system defines a block, it will save the value of the local variable, instead of waiting for the execution to access it. Therefore, after the value of myInt is modified, it has no effect on the myInt in the block. How can this problem be solved? Use _ block, so that the block uses the variable itself rather than its value _ block int myInt = 20; void (^ printInt) (void) = ^ (void) {// output 80 NSLog (@ "% d", myInt); myInt = 30; // output 30 NSLog (@ "myInt Value % d", myInt) ;}; myInt = 80; printInt (); // output 30 NSLog (@ "% d", myInt) here; * // 2. reuse the Block Variable type typedef void (^ YLPrintBlock) (int); YLPrintBlock printInt = ^ (int num) {printf (num);} YLPrintBlock printSqrt = ^ (int num) {printf (num * num);} // 3. directly use the Block as the parameter typedef void (^ YLProcessBlock) (int); void processArray (int arr [], int lenth, YLProcessBlock process) {for (int I = 0; I
  
   

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.