C語言:通過函數指標來完成兩個數的加減乘除

來源:互聯網
上載者:User

標籤:

//

//  main.c

//  Function_pointer

//

//  Created by mac on 15/8/2.

//  Copyright (c) 2015年 bjsxt. All rights reserved.

//  要求:通過函數指標求兩個整數的和、差、積、商。

 

//知識點:函數指標就是一個指向函數的指標,通過指標指向要調用的函數來完成操作。

//切記:要被調用的函數必須和函數指標的聲明的一樣(包括:傳回值類型、參數個數和類型)

#include <stdio.h>

int add(int ,int );

int sub(int ,int );

int mult(int ,int );

int divi(int ,int );

int main(int argc, const char * argv[])

{

    int (*p)(int,int);//定義函數指標

    

    p = add;//指標指向加法函數

    printf("add  = %d\n",p(20,10));

    

    p = sub;//指標指向減法函數

    printf("sub  = %d\n",p(20,10));

    

    p = mult;//指標指向乘法函數

    printf("mult = %d\n",p(20,10));

    

    p = divi;//指標指向除法函數

    printf("divi = %d\n",p(20,10));

    return 0;

}

int add(int a,int b)

{

    return a+b;

}

int sub(int a,int b)

{

    return a-b;

}

int mult(int a,int b)

{

    return a*b;

}

int divi(int a,int b)

{

    return a/b;

}

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.