C++ pointers as function parameters

In C++ syntax, the parameters of a function can not only be variables of basic types, object names, array names or references, but also pointers.

**If the pointer is used as a formal parameter, the actual parameter will pass the value to the formal parameter when calling, that is, the actual parameter and the formal parameter pointer variable will point to the same memory address. **In this way, during the running of the sub-function, the change of the data value through the formal parameter pointer also affects the data value pointed to by the actual parameter pointer.

In the C language, using a pointer as a formal parameter of a function has three functions :

① Make the actual participation formal parameter pointer point to the common memory space to achieve the purpose of two-way parameter transfer, that is, return the processing result of the function to the caller by directly processing the data in the calling function in the called function. This effect has been realized by reference in C++.

② Reduce the overhead of data transfer during function calls. This role can sometimes be achieved by reference in C++, and sometimes pointers are still required.

③ Pass the first address of the function code through the pointer to the function.

Conventionally, if the function body does not need to change the content of the object pointed to by the pointer through the pointer, it should be declared as a pointer to a constant in the parameter table, so that the constant object can also be used as a parameter of the function after its address is taken.

In programming, when a pointer or a reference can be used as a parameter in a function to achieve the same purpose, using a reference will make the program more readable.

[Example] Read in 3 floating-point numbers, and output the integer part and the decimal part respectively

void fun(float x, int* ip, float* fp)
{
    
    
	*ip = static_cast<int>(x);//取x的整数部分
	*fp = x - *ip;//取x的小数部分
}

int main()
{
    
    
	cout << "输入3个浮点数:" << endl;
	for (int i = 0; i < 3; i++)
	{
    
    
		float x,f;
		int n;
		cin >> x;
		fun(x, &n, &f);
		cout <<"第"<<i+1<<"个数的整数部分:" << n << "    小数部分:" << f << endl;
	}
	return 0;
}

operation result:

insert image description here
Result analysis:
The fun function in the program uses two pointer variables as parameters, and the main function uses the addresses of the variables as actual parameters during the calling process. When form and reality are combined, the ip value of the sub-function is the address of the int variable n in the main function. Therefore, changing the value of *ip in the sub-function will directly affect the value of variable n in the main function. There is a similar relationship between fp and the floating-point number f.

In this program, the same purpose can be achieved by using references as formal parameters. Rewrite the code as follows:

void fun(float x, int& ip, float& fp)
{
    
    
	ip = static_cast<int>(x);//取x的整数部分
	fp = x - ip;//取x的小数部分
}

int main()
{
    
    
	cout << "输入3个浮点数:" << endl;
	for (int i = 0; i < 3; i++)
	{
    
    
		float x,f;
		int n;
		cin >> x;
		fun(x, n, f);
		cout <<"第"<<i+1<<"个数的整数部分:" << n << "    小数部分:" << f << endl;
	}
	return 0;
}

operation result:

insert image description here

Guess you like

Origin blog.csdn.net/NuYoaH502329/article/details/132104691