////main.m//Lessonpointerpro////Copyright (c) 2015 Chi Hai Tao. All rights reserved.//#import<Foundation/Foundation.h>#import"Function.h"#definePI 3.1415926#defineKmul (A) (A) (A) * (b)////Conditional Compilation////Form 1//#ifdef PI//#define KPPPI 0//#else//#define KPPPPI 1//#endif//////Form 2//#ifndef KP////#define KP 3////#else////#define KP 4////#endif//////Form 3//#if 0////#define Krong 0////#else////#define Kright 1////#endif//pre-compilation directives//start with # Pre-compilation instructions: Pre-compile time to do some text and code replacement work//PI represents the macro name, 3.14 is the content that will be replaced during the precompiled periodintMainintargcConst Char*argv[]) {// //pointer, pointer variable//int a = ten;//int b = 6;//Change (&a,&b);// //Pointers and Functions// //Exercise: Exchanging two-digit values//printf ("a =%d,b =%d", A, b); /** * int a[5] = {0}; int count = sizeof (a)/sizeof (a[0]); GetArray (A,count); */ //char strarray[4][20] = {"IPhone", "IPad", "ipod", "IWatch"}; //Array of pointers /** * String char *strpinter[4] = {"IPhone", "IPad", "ipod", "IWatch"}; Printstr (strpinter,4); Sortstrarray (strpinter,4); Printstr (strpinter,4); */ //pointer data type: Student *//pointer variable:P//Initial value: &student, the address of the student is stored//byte of pointer variable p: 8 bytes////Student Student = {"Chihaitao", ' m ', 23,100};// //Student *p = &student; //accessing struct members through pointers /** * *p equivalent to student structure variable*///printf ("%s \ n", student.name);// //printf ("%s \ n", (*p). name); /** * Way 2 CPoint c1 = {4,5}; CPoint C2 = {7,1}; CPoint *P1 = &c1; CPoint *P2 = &c2; printf ("%f", Qiujuli (P1,P2)); printf ("%s \ n", p->name);//direct access via pointers,--pointer to operator//use all member variables in the output struct variable student*/ /** * < #Description #> Student Student = {"Chihaitao", ' m ', 23,100}; Student Stuarray[5] = {{"Chichi", ' m ', 25,99}, {"Haihai", ' W ', 22,98}, {"Taotao", ' W ', 23,97}, {"Niuniu", ' W ', 22 , 98}, {"Bibi", ' W ', 23,97},}; ---pointer operation (pointer)---(struct member) Student *stu = Stuarray; The string cannot be assigned directly, and the string pointer can be assigned a value (stu+4)->name = "Fengfeng"; (stu+1)->age = 120; printf ("%s", (Stu + 4)->name); printf ("%d", (stu+1)->age); printf ("%d", stuarray[4].age); printf ("%s", m1->name); printf ("%f", Stu[4].score); printf ("%p", (Stu + 4)); printf ("%p", &stuarray[4]); In ascending order of age (int i = 0; i < 5-1; i++) {for (int j = 0;j < 5-1-I; j + +) {if (Stu + j)->age > (stu+j + 1)->age) {Student temp = * (Stu + j); * (stu + j) = * (Stu + j + 1); * (Stu + j + 1) =temp; } } } */ //Defining Macros//Normal Macro//PI represents the macro name intMul = Kmul (3+1,5); printf ("mul =%d\n", Mul); /** * Macro differs from function 1. Macros are replaced in the pre-compilation period, without any logic detection, just simple assignment, run faster than the function of 2. Macro Definition Ten does not take into account the type of the parameter 3. Parameter macros remember to add brackets 4 when defining them. Parameter macros in the Time will re-target file in the village in multiple copies, will increase the size of the target file*/#ifdef PI//If a PI is definedprintf"PI has already been defined. \ n");#elseprintf ("pi is not defined \ n");#endif#ifndef PI//If no PI is definedprintf"PI This macro is not defined \ n");#elseprintf ("It's already been defined. \ n");#endif #if1//and the condition to judge if else usage asprintf ("gifted Uniqlo was checked.");#elseprintf ("What do you mean, you don't understand?");#endif return 0;}
--------Function.h
////Function.h//Lessonpointerpro////Created by Laouhn on 15/7/27.//Copyright (c) 2015 Chi Hai Tao. All rights reserved.//#import<Foundation/Foundation.h>structstudent{Char*name; Charsex; intAge ; floatscore;}; typedefstructStudent Student;typedefstruct{ Char*name; Charsex; intAge ; floatscore;} Student1;structcpoint{floatx; floaty;}; typedefstructCPoint CPoint;voidChangeint*,int*);voidGetArray (int*,int);voidPrintint*,int);voidSortarray (int*,int);voidPRINTSTR (Char*[],int);voidSortstrarray (Char*[],int);floatQiujuli (CPoint *,cpoint *);
Function.m
////function.m//Lessonpointerpro////Created by Laouhn on 15/7/27.//Copyright (c) 2015 Chi Hai Tao. All rights reserved.//#import"Function.h"voidChangeint*a,int*b) { inttemp = *A; *a = *b; *b =temp;}voidGetArray (int*p,intcount) { for(inti =0; I < count; i++) { * (p + i) = Arc4random ()% ( --Ten+1) +Ten; } print (P,count); Sortarray (P, count); Print (P, count);}voidPrintint*a,intcount) { for(inti =0; I < count; i++) {printf ("%d", * (A +i)); } printf ("\ n");}voidSortarray (int*a,intcount) { for(inti =0; I < count-1; i++) { for(intj =0; J < Count-i-1; J + +) { if(* (A + j) > * (A + j +1)) { inttemp = * (A +j); * (A + j) = * (A + j +1); * (A + j +1) =temp; } } }}voidPRINTSTR (Char*b[],intcount) { for(inti =0; I < count; i++) {printf ("%s", * (b +i)); }}voidSortstrarray (Char*str[],intcount) { for(inti =0; I < count-1; i++) { for(intj =0; J < Count-1I J + +) { if(strcmp (Str[j],str[j +1]) >0) { Char*temp =Str[j]; STR[J]= Str[j +1]; Str[j+1] =temp; } } }}floatQiujuli (CPoint *a,cpoint *b) { floatx = FABSF (a->x-b->x); floaty = FABSF (a->y-b->y); returnSqrtf (x*x + y*y);}
iOS pointer the next day