Advanced pointers
Pointer to struct variable, called struct-body pointer
You can use the to point to content.
%p Print Address
void Plenth (CPoint *p1,cpoint *p2)
Find the distance between two points with the root function sqrt () and the square function pow (,)
{
float a = sqrt (Pow ((p1->x-p2->x), 2) +pow ((P1->y-p2->y), 2));
printf ("Two point distance is %.2f\n", a);
}
//code in MAIN.M#import <Foundation/Foundation.h>#import"Lesson.h"#import"Cpoint.h"intMainintargcConst Char*argv[]) {Student Stu= {"Xiao Wang Tou", -, +,'m'}; Student*p = &Stu; P->name[0] -= +;//find the first element of name to capitalizeprintf"%s\n",p->name); //loop to underline whitespace characters for(inti =0; I<strlen (P->name); i++) { if(p->name[i]==' ') {p->name[i]='_'; }} printf ("%s",p->name);//strcpy ((*p). Name, "Xiaoliu");//strcpy (p->name, "Hello");//printf ("%lu%lu%s\nr", sizeof (Stu), sizeof (p), (*p). name);//printf ("%p%p",& (P->name), p);//Cpoint point1 = {1.25,2.36};//Cpoint *q = &point1;//printf ("%.2f%.2f\n", q->x,q->y);//two different forms//printf ("%.2f%.2f", (*q). x, (*q). y);//Cpoint point2[]={{5,6},{8,10}};//Cpoint *r = Point2;//float num=sqrt ((r->x-(r+1)->x) * (r->x-(r+1)->x) + (r->y-(r+1)->y) * (r->y-(r+1)->y));//printf ("%.2f", num);//mysqrt (point2); return 0;}//code in Cpoint.htypedefstructcpoint{floatx; floaty;} Cpoint;voidmysqrt (Cpoint point2[]);//code in CPOINT.M#import"Cpoint.h"voidmysqrt (Cpoint point2[]) {Cpoint*r =Point2; floatNum=sqrt ((r->x-(r+1)->x) * (r->x-(r+1)->x) + (r->y-(r+1)->y) * (r->y-(r+1),y)); printf ("%.2f", num);}//code in Lesson.h//create a student's structural bodytypedefstructstudent{Charname[ -];//name intNum//School Number intAge//Age CharSex//Sex}student;
An array of struct bodies.
Stu Stus[3] = {0};
Stu *p = &stus;
* (p+1) num of Num's second student
Find sex for male students, score plus 10, more than 100 write 100
Adding score members to the student structure
//Lesson.h Filetypedefstructstudent{Charname[ -];//name intNum//School Number intAge//Age CharSex//Sex floatScore;//score}student;voidManscore (Student *stud,intcount);//lesson.m File//Sex for male students score plus 10, if more than 100, score ofvoidManscore (Student *stud,intcount) { for(intI=0; i<count; i++) { if((stud+i)->sex=='m') {(Stud+i)->score+=Ten; if((stud+i)->score>= -) {(Stud+i)->score= -; }} printf ("%.2f", (Stud+i)score); }}//Main functionStudent arr[3]={{"xiaoming",1, -,'m', the},{"Xiaobai",2, -,'F', -},{"Xiaoxiao",3, +,'m', About}}; Student*p =arr;//for (int i=0; i<3; i++) {//if ((p+i)->sex== ' m ') {//(p+i)->score+=10;//if ((p+i)->score>=100) {//(p+i)->score=100;// }// }// //printf ("%.2f", (p+i)->score);// }Manscore (P,3);
Third, pre-compilation instructions
Macro definitions are used more in actual development
Replace at Precompilation (pre-compilation)
Macro name naming rules: pure capitalization or k + hump
#define KMAX 100
Eg:max or KMax
A macro is a symbolic constant that is not a variable making substitution does not make a grammar check followed by no semicolon;
Macros with parameters
#define MUL (A, A) ((A) * (B))
#define Ksum (A, b) a*bint main (intconstChar * argv[] ) { int a = ksum (3+5,6-3); // 3+5*6-3 printf ("%d", a); // Output
Conditional compilation
Function: Compile different code according to different conditions.
There are three forms of conditional compilation
1. #ifdef identifiers
Code Snippet 1
#else
Code Snippet 2
#enfif
// If you define a macro AAA #ifdef AAA printf (" this piece of code is compiled "); #else printf (" this piece of code is compiled "); #endif // End of compilation ends with #endif
2. #ifndef identifiers
Code Snippet 1
#else
Code Snippet 2
#endif
3. #if constant expression
Code Snippet 1
#else
Code Snippet 2
#endif