Review of the data structure (C ++ part)
1. The transmission parameters
1) value is passed
The parameter is a copy of the argument, the change parameter value does not affect the value of the argument
It is passed from a solid to a unidirectional reference parameter
Values of the parameters can only be passed, can not be heard
pass in the copy
2) pass by reference
pass in the variable itself doesn't have to make copy save space
Alias is the reference argument, equivalent to the operation of a reference direct operation argument
Syntax: type name reference name = & target variable int & a = b
To [4] = b type (the reference name &) reference to an array int (& a) [number of elements in the array] = array name
REFERENCE pointer int (* &) a = b type (& *) = application name pointer
Reference is created that must be initialized, a reference type must be the same type of target
int main () {
int a=6;
int b=8;
void passByValue(int x)
{x=9;}
void passByReference(int &x)
{x=10;}
int a; &a; // address of a
int& a = b; // the "&" now means you're declaring a reference to "b".
Of course, when declaring a refence, you must immidiately assign it to something. References must always refer to something, they can't refer to nothing. So...
int& a; // not valid.
int& a = b; // valid.
To use it with function, you need to declare the function this way, to accept referneces: void function(int& a); // takes a reference to an int.
3) passing a pointer
Parameter points to the address of the argument pointer, pointing to the parameter when the operation is equivalent to the operation of the argument itself
To summarize pointer references and similarities and differences:
★ same point:
● concepts are addresses;
A pointer to a memory, it is referred to the content of the memory address; and references a block of memory is the alias.
★ different points:
● pointer is an entity, but only reference is an alias;
basic difference
pointer have the freedom to move around and point to different value
the reference is assigned one time and it just become the reference to that location in the memory.
https://www.youtube.com/watch?v=sxHng1iufQE
● reference can only be defined when the initialization time, after immutable; pointer variable; references "single-mindedness" pointer "rolling stone";
● no const reference, there is a pointer const, const pointer immutable; (not specifically refer to this int & const a form, and there is const int & a, i.e., the former refers to an alias reference itself may not change, it is, of course, the this need not form, which refers to the value of the reference refers not change)
● null reference can not be empty, the pointer;
● "sizeof reference to" get the size of the variable pointed (object), and "the sizeof pointer" size of a pointer is obtained itself;
void swapint(int *a,int *b)
{
int temp;
temp=*a;
a=*b;
*b=temp;
}
void swapint(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
若定义:char s[20]="programming",*ps=s ;
则不能表示字符‘o’的是() 。
-
ps+2
-
s[2]
-
ps[2]
-
*(ps+2)
ps+2指向o字符的地址 而不是'o'
2.函数指针
void traverse(void (*visit)(List_Entry&))
https://zhuanlan.zhihu.com/p/37306637
函数返回类型(*指针变量)(形参列表)
int (*fp)(int a)
函数指针的应用
void List::traverse(void(*visit)(int &))
{for(int i=0;i<count;i++)
(*visit)(entry[i]);}
void print(int &x)
{cout<<x<<endl;}
mylist.traverse(print);
3.内存分配