c++(三)函数原型、函数参数、函数返回值、函数指针

0.函数原型是什么

提前告诉编译器函数原型的存在,防止捕获错误。说白了,就是预先声明函数形式,防止在函数没有声明前就调用提示找不到的问题

1.函数原型的必要性

编译器正确处理函数的返回值;

编译器检查参数数目是否正确;

编译器检查参数类型是否正确;

唯一避免使用的方法是在使用调用函数前定义,但是往往不总是可行的。

2.按不同功能设计函数原型,处理数组

要修改数组内容:void f_modify(double d[], int n);

不需要修改数组:void f_no_change(const double[], int n);

3.指针和const

总结:const修饰的变量,不能定义指向它的指针

          const修饰的指针,不能通过指针修改原变量的值

          const类型的指针,不能移动指针指向别的变量

    int gorp = 16;
    int chips = 12;
    const int * p_snack = &gorp;

    * p_snack = 20;//禁止修改指向p_snack的值
    p_snack = &chips;//p_snack可以指向另一个变量


    int dog = 16;
    int cat =12;
    int * const p_s = &dog;
    *p_s = 20;//可以通过指针修改常规变量的值
    p_s = &cat;//const指针不能指向别的变量

4.函数形参是二维数组,函数原型定义?

int sum(int (*ar2)[4], int size);

int main() {
    using namespace std;
    int a[2][4] = {
            {1, 2, 3, 4},
            {6, 6, 6, 6}
    };
    int total = sum(a, 2);
    cout << "total: " << total;
    return 0;
}

int sum(int ar2[][4], int size) {
    int total = 0;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < 4; ++j) {
            total += ar2[i][j];
        }
    }
    return total;
}

5.函数参数是char数组,函数原型定义?

unsigned int c_in_str(const char *str, char ch);

int main() {
    using namespace std;
    char *mm = "minimum";
    char ll[15] = "will";
    int c1 = c_in_str(mm, 'm');
    int c2 = c_in_str(ll, 'l');
    cout << "mm:" << c1 << endl;
    cout << "ll:" << c2 << endl;
    return 0;
}

unsigned int c_in_str(const char *str, char ch) {
    unsigned int count = 0;
    while (*str) {
        if (*str == ch) {
            count++;
        }
        str++;
    }
    return count;
}

6.函数返回值是char数组

创建一个空数组,其长度是数组长度+1,最后一位存储\0.循环写入成输入的字符:

char * buildstr (char c, int n);
int main() {
    using namespace std;

    char * c = buildstr('B',10);
    cout << c;
    return 0;
}

char * buildstr(char c, int n){
    char * pstr = new char [n+1];
    pstr[n] = '\0';
    while(n-- > 0){
        pstr[n] = c;
    }
    return pstr;
}

7.函数参数是结构指针,函数原型定义?

struct time {
    int hours;
    int min;
};
void show_time(struct time *t);
void show_time(struct time *t) {

}

8.函数指针的使用

 这一块稍微复杂一点,可能有点绕。其实本质就是函数指针的声明和使用方法这两点。

首先我们知道,指针是存储变量地址的,函数也有地址,存放函数地址的就是函数指针。

函数指针是一个变量,指向不同的函数,可以调用其指向的函数,比如回调函数、策略模式和表驱动方法。通常声明形式如下:

返回值类型 (*函数指针名)(参数列表)。
例如:int (*functionPtr)(int, int);创建了一个返回值int,参数是两个int值的,名为functionPtr的函数指针,使用如下方式使用:
functionPtr = &add;
double result = 0.0;
double *add(const double *d, int e);
double *(*calculate)(const double *d, int e);
int main() {
    using namespace std;
    calculate = &add;
    double age = 20.5;
    double *num = calculate(&age, 10);
    cout << "num:" << *num << endl;
    return 0;
}
double *add(const double *d, int e) {
    double d1 = *d + 1;
    result = d1 + double(e);
    return &result;
}

以上代码定义了一个函数指针,名为calculate。返回值是double类型的指针,参数一是double指针,参数二是int类型。调用流程,首先把定义好的add方法赋值给函数指针,然后把参数age通过&取址,连同int类型参数10一起传给函数指针,计算结果最后是31.5

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值