[C/C++ Puzzles] *, **, :: , &, *&, **& and function parameter transfer -> Detailed explanation of the seven
foreword
Recently, pointer learning is still easy to get lost, so reorganize and learn!
Learn about it!
The reason why the C language is named "C" is to take the second letter of "BCPL" (the first letter is used to name the B language that was invented before). When the development of the C language reached its peak, a version called C with Class appeared, which was the earliest version of C++. The class keyword and class were added to the C language. At that time, there were many versions of C that hoped to be added to the C language. The concept of class; later, the C standard committee decided to give this version of C a new name. At that time, a lot of names were collected, and finally the opinion of one of them was adopted, and the ++ operator in the C language was used to reflect it. The progress of the C language, so it is called C++, and the C++ Standards Committee was established.
1. Usage of * in C/C++
- Simplest multiplication:
a*b;
- Code comments:
/**/
- pointer:
A pointer is an address to a variable.
Example:
int main()
{
int a = 3;
int *b = &a;
cout << "a:" << a << endl;
cout << "b:" << b << endl;
*b = 10;
cout << "&a:" << &a << endl;
cout << "b:" << b << endl;
system("pause");
}
result:
a:3
b:00EFFE28
&a:00EFFE28
b:00EFFE28
a:10
analyze:
b is a pointer to a, pointing to the address of a, both point to the same memory space (that is, a and b are connected, as long as the value of *b is modified, the value of a also changes).
2. Usage of & in C/C++
- bitwise operators
a&b
- Logical AND &&
if((a==0)&&(b==0)){
}
- Quote & (most important)
A reference is an alias for a variable.
It can be said that the a variable is renamed to b, that is:
&b=a
Example:
//引用
int main()
{
int a = 3;
int &b = a;
int c = a;
cout << "a:" << a << endl;
cout << "b:" << b << endl;
cout << "c:" << c << endl;
b = 10;
cout << "a:" << a << endl;
cout << "b:" << b << endl;
cout << "c:" << c << endl;
cout << "&a:" << &a << endl;
cout << "&b:" << &b << endl;
cout << "&c:" << &c << endl;
system("pause");
}
result:
a:3
b:3
c:3
a:10
b:10
c:3
&a:0019FD74
&b:0019FD74
&c:0019FD5C
analyze:
& Quotes: For example, how many nicknames a person has, but they all refer to that person, and the same goes for quotes. If the value of b changes, it also means that the value of a has changed.
3. Function parameters
3.1 Functions pass in common parameters
//函数传入普通参数
void fun(int a,int b)
{
int c = 0;
c = a;
a = b;
b = c;
}
int main()
{
int a = 1;
int b = 2;
cout << a << "," << b << endl;
fun(a, b);//a,b交换
cout << a << "," << b << endl;
system("pause");
return 0;
}
result:
1,2
1,2
analyze:
The function passed in is a formal parameter, and it will not change the address of a and b in main(), that is, it will not change the value of a and b.
3.2 Functions pass in pointer parameters
//函数传入指针参数
void fun(int *a, int *b)
{
int c = 0;
c = *a;
*a = *b;
*b = c;
}
int main()
{
int a = 1;
int b = 2;
cout << a << "," << b << endl;
fun(&a, &b);//a,b交换
cout << a << "," << b << endl;
system("pause");
return 0;
}
result:
1,2
2,1
analyze:
The parameter passed to the function is a pointer, that is, an address. The exchange of a and b in the function is the exchange of the value of the address. Finally, the values of a and b in main() also change.
3.3 Passing in function parameters by reference
Simple – easy to use, recommended!
//引用传入函数参数
void fun(int &a, int &b)
{
int c = 0;
c = a;
a = b;
b = c;
}
int main()
{
int a = 1;
int b = 2;
cout << a << "," << b << endl;
fun(a, b);//a,b交换
cout << a << "," << b << endl;
system("pause");
return 0;
}
result:
1,2
2,1
analyze:
In essence, the a and b variables in main() are renamed, that is: a, b in the function, and the addresses of a and b in the function are the same as the addresses in main(). If the values of a and b in the function change, then the values of a and b in main() also change.
The essence of the citation is :The C++ internal implementation is a pointer constant.
Please move to : [C++ Core Programming] Reference (2)
4. Usage of :: in C/C++
:: refers to the scope operator, or scope qualifier.
With two colons together, this symbol has two names in C++:
- unary scope resolution operator
- Binary Scope Resolution Operator
When used as a unary scope resolution operator, it can be used in the following cases:
int a = 3;
int main()
{
int a = 4;
cout << ::a << endl; // 这时输出的是全局变量a
cout << a << endl; // 这时输出的是局部变量a
return 0;
}
When it is used as a binary scope discrimination operator, it is mainly used to bind the member functions of the class in the source file, such as:
a.h
class a
{
public:
void fun1();
int fun2();
private:
int x;
};
a.cpp
#include "a.h"
void a::fun1()
{
.......
}
int a::fun2()
{
......
}
Binding member functions in a class:
class Building
{
public:
Building();
public:
string m_SittingRoom; //客厅
private:
string m_BedRoom;//卧室
};
Building::Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
5. Usage of ** in C/C++
Simply put, it means a two-dimensional pointer (double pointer).
have to be aware of is. A single * can represent a one-dimensional array, then ** can represent a special array composed of arrays: a two-dimensional array.
This is a pointer to a pointer, such as: int * *p;
p is a pointer variable that points to pointer data.
E.g:
int i=5;
int *p;
p=&i;
int **q;
*q=p;
printf("%d",**q); //输出结果是5
6. Usage of *&, **& in C/C++
Quote &, as we all know, what does the *& and **& mean respectively?
int *&p;
int **&p;
其实这两个*& 和 **&是表示引用,*&表示指针的引用,**&表示指针的指针的引用。
Example:
void foo(int*& x, int**& y) {
// modifying x or y here will modify a or b in main
}
int main() {
int val = 42;
int *a = &val;
int **b = &a;
foo(a, b);
return 0;
}
Modifying x and y in the calling function will directly affect the values of a and b in the main function. because they are referenced.
void pass_by_value(int* p)
{
//Allocate memory for int and store the address in p
p = new int;
}
void pass_by_reference(int*& p)
{
p = new int;
}
int main()
{
int* p1 = NULL;
int* p2 = NULL;
pass_by_value(p1); //p1 will still be NULL after this call
pass_by_reference(p2); //p2 's value is changed to point to the newly allocate memory
return 0;
}
come on!
grateful!
effort!