# * and usage of C and C++

• 2020-11-26 18:56:31
• OfStack

* in C/C++

1 > The simplest multiplication:

a*b;

2 > Can be commented:

/**/

3 > Pointer :(most important)

A pointer is the address of a variable
Simple example analysis:

``````
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");
}``````

Results:

[

a: 3
b: 00 EFFE28
& a: 00 EFFE28
b: 00 EFFE28
a: 10

]

Analysis:
b is a pointer to a and points to the address of a. (That is, a is linked to b. As long as the value of *b is changed, the value of a will also be changed.)

C/C + + & The use of the

1 > An operator

a & b

2 > Logic and & &

``````
if((a==0)&&(b==0)){}``````

3 > reference & (Most important)

The reference can be said to change the variable a by a name of b, that is:

``````
&b=a``````

Simple example analysis:

``````
// reference
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");
}``````

Results:

[

a: 3
b: 3
c: 3
a: 10
b: 10
c: 3
& FD74 a: 0019
& FD74 b: 0019
& FD5C c: 0019

]

Analysis:
& Quotes: For example, how many nicknames a person has, but all refer to that person, and the same is true of quotes. If the value of b is changed, then the value of a is changed.

Argument 1 of the (core) function > The function passes in normal parameters

``````
// The function passes in normal 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 exchange
cout << a << "," << b << endl;
system("pause");
return 0;
}``````

Results:

[

1,2
1,2

]

Analysis:
The function passes in a formal parameter, which does not change the address of a,b in main(), i.e., does not change the value of a,b.

2 > The function passes in a pointer argument

``````
// The function passes in a pointer argument
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 exchange
cout << a << "," << b << endl;
system("pause");
return 0;
}``````

Results:

[

1,2
2,1

]

Analysis:
The arguments to the function pass in a pointer, or address. The swapping of a,b in the function is the swapping of addresses. Finally, a in main(), the value of b changes.

3 > Pass in a function argument by reference (simple wok)

``````
// Pass in function parameters by reference
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 exchange
cout << a << "," << b << endl;
system("pause");
return 0;
}``````

Results:

[

1,2
2,1

]

Analysis:
Essentially, the a and b variables in main() are renamed, that is, the a,b, a,b address in the function is the same as the address in main(). If the values of a and b in the function change, then the values of a and b in main() also change.

This post summarizes some of the most confusing and difficult points of initial contact with c/c++ Pointers, addresses, and references. At the same time, it is the most important knowledge.

Related articles: