// arrfun4.cpp -- functions with an array range#include<iostream>constint ArSize =8;intsum_arr(constint* begin,constint* end);intmain(){
using namespace std;int cookies[ArSize]={1,2,4,8,16,32,64,128};// some systems require preceding int with static to// enable array initializationint sum =sum_arr(cookies, cookies + ArSize);
cout <<"Total cookies eaten: "<< sum << endl;
sum =sum_arr(cookies, cookies +3);// first 3 elements
cout <<"First three eaters ate "<< sum <<" cookies.\n";
sum =sum_arr(cookies +4, cookies +8);// last 4 elements
cout <<"Last four eaters ate "<< sum <<" cookies.\n";return0;}// return the sum of an integer arrayintsum_arr(constint* begin,constint* end){constint* pt;int total =0;for(pt = begin; pt != end; pt++)
total = total +*pt;return total;}
函数指针
格式:数据类型(*函数名xxx)(argument1,argument2,…);
也可以这样写:先编写这种函数的原型,然后用(*xxx)替换这种函数名
doublepam(int);double(*pf)(int);
pf = pam;// pf now points to the pam() functiondouble x =pam(4);// call pam() using the function namedouble y =(*pf)(5);// call pam() using the pointer pf
pf与(*pf)等价!!!
double y =pf(5);// also call pam() using the pointer pf
c语言链接:
extern"C"voidspiff(int);// use C protocol for name look-up
c++语言链接:
externvoidspoff(int);// use C++ protocol for name look-upextern"C++"voidspaff(int);// use C++ protocol for name look-up