iOS學習筆記---c語言第九天

來源:互聯網
上載者:User

標籤:style   blog   color   使用   strong   檔案   

進階指標

 

指向結構體變數的指標,稱為結構體指標

 

可以使用->指向內容。

%p列印地址

void pLenth(cPoint *p1,cPoint *p2)

//求兩點間的距離  用的開方函數sqrt()和平方函數pow(,)

{

    float a = sqrt(pow((p1->x-p2->x), 2)+pow((p1->y-p2->y), 2));

    printf("兩點距離為%.2f\n",a);

}

//main.m中代碼#import <Foundation/Foundation.h>#import "Lesson.h"#import "Cpoint.h"int main(int argc, const char * argv[]){    Student stu = {"xiao wang tou",18,32,‘m‘};    Student *p = &stu;    p->name[0] -= 32;//找到name第一個元素變大寫    printf("%s\n",p->name);    //迴圈把空白字元變底線    for (int i = 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);//兩種形式//    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;}//Cpoint.h中代碼typedef struct cpoint{    float x;    float y;}Cpoint;void mysqrt(Cpoint point2[]);//Cpoint.m中代碼#import "Cpoint.h"void mysqrt(Cpoint point2[]){    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);}//Lesson.h中代碼//建立一個學生的結構體typedef struct student{    char name[20];//姓名    int num;      //學號    int age;      //年齡    char sex;     //性別}Student;

 

結構體數組。

stu stus[3] = {0};

stu *p = &stus;

*(p+1).num  第二個學生的num

 找出性別為男的學生,成績加10,超過100寫100

在student結構體中加score成員

 

//Lesson.h檔案typedef struct student{    char name[20];//姓名    int num;      //學號    int age;      //年齡    char sex;     //性別    float score;  //分數}Student;void manscore(Student *stud,int count);//Lesson.m檔案//性別為男的學產生績加10,如果超過100,成績為100void manscore(Student *stud,int count){    for (int i=0; i<count; i++) {        if ((stud+i)->sex==‘m‘) {            (stud+i)->score+=10;            if ((stud+i)->score>=100) {                (stud+i)->score=100;            }        }                printf("%.2f ",(stud+i)->score);    }}//主函數   Student arr[3]={{"xiaoming",1,18,‘m‘,95},{"xiaobai",2,20,‘f‘,90},{"xiaoxiao",3,19,‘m‘,56}};    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);

 

三、先行編譯指令

宏定義     實際開發中用的比較多

先行編譯時進行替換(編譯前)

宏名命名規則:   純大寫  或者   k+駝峰

#define kMax 100

eg:MAX 或者  kMax

宏是符號常量不是變數   製作替換不做語法檢查  後面沒有分號;

帶參數的宏

#define MUL(A,B) ((A)*(B))

#define kSum(A,B) A*Bint main(int argc, const char * argv[]){    int a = kSum(3+5,6-3);//3+5*6-3    printf("%d",a);//輸出30

 條件編譯

作用:按不同的條件,編譯不同的代碼。

條件編譯  有三種形式

1.#ifdef  標識符

程式碼片段1

#else

程式碼片段2

#enfif

    //如果定義了宏AAA#ifdef AAA    printf("這段代碼被編譯");#else    printf("這段代碼才被編譯");#endif//編譯結束以#endif結束

2.#ifndef 標識符

程式碼片段1

#else

程式碼片段2

#endif

3.#if  常量運算式

程式碼片段1

#else

程式碼片段2

#endif

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.