argument 实参
parameter形参
因为函数中 love(char a[])传递的实质为char *a 是a的地址,所以在函数中改变a会改变a的值
#include<iostream>
#include<cstring>
using namespace std;
void mylove(char *a)
{
for (int i = 0; i < 6; i++)
{
a[i] = a[i] + 1;
}
}
int main()
{
char a[20] = "hknudt";
cout << "I have words:" <<a<< endl;
mylove(a);
cout << "that is:"<<a;
}
可以看到数组的值改变了 (在函数形参位置加const在你尝试修改数组的时候会报错)
Const的变量,其指针的指针也必须是const类型的
还有一个问题就是string声明的数组是一个字符串常量,其单独的值不可以修改,new新建的数组只能一个一个赋值,要在初始阶段赋值最方便的还是数组
array<string,num> name;利用array方法数组
指向函数的指针
double pam(int);
double(*pf)(int);
pf=pam;
double x=pam(4);
double y=(*pf)(5);
pf(5);
函数指针例子
#include<iostream>
#include<cmath>
using namespace std;
double my(int t)
{
return t * (log(t)) / (log(2));
}
double yours(int t)
{
return t * t;
}
void show(int t,double(*pf)(int))
{
cout << "It will take:" << pf(t) << "times" << endl;
}
int main()
{
cout << "tell me how much numbers:";
int t = 0;
cin >> t;
show(t, my);
show(t, yours);
return 0;
}