文章目录
dereference operator (&)
pointer to const can’t use to change the value of its object.
a reference is not a object. Instead, a reference (&) is a another name for an already existing object. All in all, reference must be initialized.
A pointer is an object in memory.
decltype
decltype returns the type of its operand.
decltype(f()) sum = x; // sum has whatever type f returns.
ted = &andy;
int *p = nullptr; means *p = 0 ;
const double pi = 3.14 ; pi is constant and not change.
double *p = π wrong because of ptr is ordinary pointer.
const double *ptr = π right,
*ptr = 1 ; wrong .
assign the addresses of andy to ted.
if(!p){}
#include<iostream>
using namespace std;
const double * f1(const double * ar, int n);
const double * f2(const double ar[], int n);
const double * f3(const double ar[], int n);
int main()
{
double av[3] = { 1112.3, 1542.6, 2227.9 };
const double *(*p1)(const double *, int) = f1;
auto p2 = f2;
cout << "使用指向函数的指针:\n"
<< ",地址值:\n"
<< (*p1)(av, 3) << ": " << *(*p1)(av, 3) << endl
<< p2(av, 3) << ": " << *p2(av, 3) << endl;
const double *(*pa[3])(const double *, int) = { f1, f2, f3 };
auto pb = pa;
cout << "\n使用数组的指针来函数:\n"
<< ",地址值\n";
for (int i = 0; i < 3; i++)
cout << pa[i](av, 3) << ": " << *pa[i](av, 3) << endl;
cout << "\n使用指向一个函数的指针的指针:\n"
<< ",地址值\n";
for (int i = 0; i < 3; i++)
cout << pb[i](av, 3) << ": " << *pb[i](av, 3) << endl;
cout << "\n使用指针的functers数组:\n"
<< ",地址值\n";
auto pc = &pa;
cout << (*pc)[0](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)[2])(av, 3) << endl;
system("pause");
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;
}
string & rs; worong
string s("OK");
string& rs = s; right, rs refer to s
struct
#include <iostream>
using namespace std;
struct student
{
char name[20];
int age;
double hight;
};
int main()
{
struct student name_li;
student name_li ; // C++ only
student name_li={"zhang san",18,180.1};
student name_zhang {"zhang san",18,180.1};
struct name_wang {}; // all value is zero.
name_li = name_wang //assign a structure to another
}
cout <<name_li.name << " " << name_li.age <<endl;
}
output:
"zhang san" 18
sturct array
struct student gift[100];
student gift[2] =
{
{"baby", 29, 21.5},
{"Godzilla", 15,22.5}
};
cout<<gift[0].name << gift[1].name<< endl;
pass by sturct data
#include <iostream>
using namespace std;
struct travel_time
{
int hours;
int mins;
};
travel_time sum(travel_time t1, travel_time t2);
void show_travel(travel_time total);
int main()
{
struct travel_time t1 = { 5, 55 };
struct travel_time t2 = { 4, 5 };
travel_time total = sum(t1, t2);
show_travel(total);
return 0;
}
travel_time sum(travel_time t1, travel_time t2)
{
travel_time total;
total.mins = (t1.mins + t2.mins) % 60;
total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) % 60;
return total;
}
void show_travel(travel_time total)
{
cout << total.hours << " " << total.mins << endl;
}
pass by address as a struct
travel_time sum(travel_time *t1, travel_time *t2, travle *total)
{
total->mins = (t1->mins + t2->mins) % 60;
total->hours = t1->hours + t2->hours + (t1->mins + t2->mins) % 60;
}
void show_travel(const travel_time *total)
{
cout << total->hours << " " << total->mins << endl;
}