【學習ios之路:C語言】指標函數

來源:互聯網
上載者:User

標籤:style   blog   ar   io   color   os   使用   sp   for   

  指標函數int max(int a, int b);

     *   函數指標:指向一個函數的指標變數.‘

     *   函數的類型:返回值類型+參數

     *   函數指標變數類型: int (*)(int x, int y)

     *   指標變數的名字:max

     *   初始值:NULL

     *   max 可以指向一個返回值是int型並且有兩個int型的參數的函數.

應用:學生結構體按照姓名,年齡,分數升序排序

1.函數的聲明

typedef struct student{    char name[20];    int age;    float score;}Student;typedef BOOL (*ALL)(Student, Student);typedef struct nameFunctionPair{    char name[10];//名字    ALL p; //對應的函數地址}NameFunctionPair;//年齡BOOL compareByAge(Student stu1 , Student stu2);//成績BOOL compareByScore(Student stu1 , Student stu2);//姓名BOOL compareByName(Student stu1 , Student stu2);//排序void sortStudent(Student *p, int cont , ALL a);//輸出所有學生的資訊void outputStudent(Student *p, int count);//從匹配表中匹配到對應的函數地址.ALL matchFunctionByName(NameFunctionPair *q, int count ,char *name);//*p接收匹配表數組.count.接受匹配表數組的元素個數.name.接受使用者輸入的內容.<span style="color:#ff0000;"></span>


2.函數的實現

//輸出所有學生的資訊void outputStudent(Student *p ,int count) {    for (int i = 0; i < count; i++) {        printf("%s,%d,%.1f\n",p[i].name,p[i].age,p[i].score );    }}//年齡BOOL compareByAge(Student stu1 , Student stu2){    return stu1.age > stu2.age;}//成績BOOL compareByScore(Student stu1 , Student stu2) {    return stu1.score > stu2.score;}//姓名BOOL compareByName(Student stu1 , Student stu2) {    return strcmp(stu1.name, stu2.name) > 0;}//排序void sortStudent(Student *p, int count ,ALL a) {        for (int i = 0; i < count - 1; i++) {        for (int j = 0; j < count - 1 - i; j++) {            if (a(p[j], p[j + 1])) {                Student temp = p[j];                p[j] = p[j + 1];                p[j + 1] = temp;            }        }    }    }ALL matchFunctionByName(NameFunctionPair *q, int count ,char *name) {        for (int i = 0; i < count ; i++) {        if (strcmp(q[i].name, name) == 0) {            return q[i].p;        }    }    return NULL;//如果沒有匹配成功返回NULL.}


3.函數實現

     Student stu[5] = {        {"zhangs", 24, 78.9},        {"xiaom", 18, 45.8},        {"xiaozhen", 19,56.0},        {"heh", 20, 64.8},        {"frak", 19, 79.0}    };        NameFunctionPair name[3] = {        {"age", compareByAge},        {"score", compareByScore},        {"name", compareByName}    };        while (YES) {    char temp[10] = {0};    printf("name-姓名,age-年齡,socre-分數:\n");    scanf("%s",temp);    //根據使用者輸入內容匹配到對應的函數    ALL al = matchFunctionByName(name, 3, temp);    if (al != NULL) {//while(p == NULL)        //根據匹配到的內容進行排序.        sortStudent(stu, 5, al);        outputStudent(stu, 5);    } else if (al == NULL) {        printf("你輸入的字串不正確!請重新輸入\n");            }}

二.回呼函數應用.

1.隨機產生一個10個元素的數組,找到3的倍數,並將其值修改成0.(注意:修改數值使用回掉函數處理)

1.函式宣告

typedef int (*ZERO)(int,int);

//隨機10個元素的數組void getTenNumber(int *p, const int count);//遍曆去除元素void outputNumber(int *p, const int count);//找到3的倍數,並且賦值為0void findNumberToZero(int *p, const int count, ZERO z, int n);//找n的倍數,int copyZero(int number,int n);
2.方法實現

//隨機10個元素的數組void getTenNumber(int *p, const int count) {    for (int i = 0; i < count; i++) {        p[i] = arc4random() % (40 - 10 + 1) + 10;    }}//遍曆取出元素的個數void outputNumber(int *p, const int count) {    for (int i = 0; i < count; i++) {        printf("%d ",p[i]);    }}//找到3的倍數,並且賦值為0void findNumberToZero(int *p, const int count ,ZERO z,int n) {    for (int i = 0; i < count; i++) {        if (z(p[i],n) == 0) {            p[i] = 0;        }    }}int copyZero(int number ,int n) {    if (n == 0) {        return NO;    }    return number % n;}

3.函數調用

    int arr[10] = {0};    getTenNumber(arr, 10);    outputNumber(arr, 10);    //4對應找到3的倍數,    findNumberToZero(arr, 10, copyZero, 4);    printf("\n");    outputNumber(arr, 10);







【學習ios之路:C語言】指標函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.