zoukankan      html  css  js  c++  java
  • cpp(第七章)

    1.c++提供了3种表示c—风格字符串方法:字符数组,字符串常量,字符串指针。其中字符数组并不一定是字符串,以空值字符''来结束的字符数组时字符串。

    2.函数参数为数组时,虽然减少了时间和内存的使用,但是却使原始数据破坏风险增大,而这时候我们在函数参数数组上加上限定符const,使函数不能修改数组内容。

    3.函数指针,函数名即为地址,可以定义一个参数为函数指针来传递函数地址,从而可以在函数中使用另一个函数。

    4.

    #include <iostream>
    #include <cstdio>
    
    const double *f1(const double ar[],int);
    const double *f2(const double[],int);
    const double *f3(const double * ,int);
    typedef const double *(*p_fun)(const double*,int );
    int main()
    {
        using std::cout;
        using std::endl;
        using std::cin;
        double av[3]= {1112.3,1542.6,2227.9};
    //    const double *(*pa[3])(const double *,int)= {f1,f2,f3};
    //    auto pb= pa;
    //    printf("%p
    ",f1(av,3));
    //    printf("%p
    ",pa[0]);
    //    printf("%p
    ",*pb[0]);
        //printf("%p
    ",*pa[2]);
        //cout<<long(f1)<<endl;
    //    const double *(*p1)(const double*,int )= f1;
        p_fun p1= f1;
        auto p2= f2;
        cout<<"using pointers to functions:
    ";
        cout<<"address value
    ";
        cout<<(*p1)(av,3)<<" : "<<*(*p1)(av,3)<<endl;
        cout<<p2(av,3)<<" : "<<*p2(av,3)<<endl;
        const double *(*pa[3])(const double *,int)= {f1,f2,f3};
        auto pb= pa;
        cout<<"
    using an array f pointer to a pointers to functions:
    ";
        cout<<" Address value
    ";
        for (int i= 0; i< 3; i++)
            cout<<pa[i](av,3)<<" : "<<*pa[i](av,3)<<endl;
        cout<<"
    using a pointer to a pointer to a function:
    ";
        cout<<" address value
    ";
        for (int i= 0; i< 3; i++)
        {
            cout<<(*pb[i])(av,3)<<" : "<<*(*pb[i])(av,3)<<endl;
        }
        cout<<"
    using pointers to an array of function pointers:
    ";
        cout<<"Address value
    ";
        auto pc= &pa;
        cout<<(*pc)[2](av,3)<<" : "<<*(*pc)[0](av,3)<<endl;
        const double *(*(*pd)[3])(const double *,int)= &pa;
        const double *pdb= (*pd)[1](av,3);
        cout<<pdb<<" : "<<*pdb<<endl;
        cout<<(*(*pd)[2])(av,3)<<" : "<<*(*(*pd))(av,3)<<endl;
        return 0;
    }
    const double *f1(const double *ar,int n)
    {
        return ar;
    }
    const double *f2(const double ar[],int n)
    {
        return ar+1;
    }
    const double *f3(const double ar[],int n)
    {
        return ar+2;
    }
  • 相关阅读:
    POJ 1141 括号匹配 DP
    881. Boats to Save People
    870. Advantage Shuffle
    874. Walking Robot Simulation
    文件操作
    861. Score After Flipping Matrix
    860. Lemonade Change
    842. Split Array into Fibonacci Sequence
    765. Couples Holding Hands
    763. Partition Labels
  • 原文地址:https://www.cnblogs.com/Call-C/p/5670463.html
Copyright © 2011-2022 走看看