C++ pointer concept, pointer variables are passed as function parameters

Pointer definition

p is a pointer, which represents the address of the stored value ;
*p is the value stored in the address .

//创建变量a,将值1存储在该变量中
int a = 1;
//创建变量pa,将变量a的地址存储在pa中
int* pa = &a;
//强调*p是一个int类型的值——C程序员
int *p;
//强调int*是一种类型——C++程序员
int* p;

Pointer variable to function parameter transfer-exchange the values ​​of two variables

The parameters of the function can be not only specific data such as integers, decimals, and characters, but also pointers to them.

Using pointer variables as function parameters can pass the address outside the function to the inside of the function , so that the data outside the function can be manipulated inside the function, and these data will not be destroyed with the end of the function.

Arrays, strings, dynamically allocated memory, etc. are all collections of a series of data . There is no way to pass all of them into the function through one parameter . You can only pass their pointers. These data sets are affected by the pointers inside the function.

……
startTest();
……

void MainWindow::startTest()
{
    
    
    qDebug() << "asd";

    int a = 1;
    int b = 2;

    swap(a, b);

    qDebug() << a << b;
}

//Qt中除了构造函数和析构函数,都要加上void或double的类型
void MainWindow::swap(int a, int b)
{
    
    
    qDebug() << a << b;
    
    int temp = a;
    a = b;
    b = temp;
    
    qDebug() << a << b;
}
asd
1 2
2 1
1 2

The values ​​of a and b did not change, and the exchange failed.

The life cycle is different. The local variables defined in the function will end after the function is run. Data can be exchanged in the swap function, but the changes cannot be displayed outside the function.

This is because the a and b inside the swap() function and the a and b inside the main() function are different variables, occupying different memory. They have no other relationship except the same name.

swap() exchanges the values ​​of a and b inside it, and does not affect the values ​​of a and b outside it (main() inside).

……
 pStartTest();
……
 
void MainWindow::pStartTest()
{
    
    
    qDebug() << "asd";
    
    int a = 1;
    int b = 2;
    
	//调用 pSwap() 函数时,将变量 a、b 的地址分别赋值给 p1、p2(都是地址)
    pSwap(&a, &b);
    
    qDebug() << a << b;
}

void MainWindow::pSwap(int* p1, int* p2)
{
    
    
    qDebug() << *p1 << *p2;
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;

    qDebug() << *p1 << *p2;
}

Although p1 and p2 will be destroyed after the function runs, its impact on external a and b is "persistent" and will not "restore to the original state" with the end of the function.

asd
1 2
2 1
2 1

Pointer variables are used as function parameters-arrays are used as function parameters

#include <stdio.h>

//C++系列专属
#include <iostream>
using namespace std;

int max(int *intArr, int len) 

//int类型的可以不用声明,但是void类型必须声明
int max(int *intArr, int len) {
    
    
	int i, maxValue = intArr[0];  //假设第0个元素是最大值
	for (i = 1; i < len; i++) {
    
    
		if (maxValue < intArr[i]) {
    
    
			maxValue = intArr[i];
		}
	}
	return maxValue;
}

int main() {
    
    
	int nums[6], i;
	int len = sizeof(nums) / sizeof(int);//可以得到长度

	//读取用户输入的数据并赋值给数组元素
	for (i = 0; i < len; i++) {
    
    
		//scanf_s("%d", nums + i);
		//不可以使用nums++,是因为是常量指针
		cin >> nums[i];//常量指针
	}

	//printf("Max value is %d!\n", max(nums, len));
	//max函数的返回值是int类型的值,可以直接写
	cout << max(nums, len);//nums是数组变量名,也就是数组的首地址

	system("pause");
	return 0;
}
12 55 30 8 93 27(enter)
Max value is 93!

The parameter intArr is just an array pointer . The length of the array cannot be obtained from this pointer inside the function. The length of the array must be passed as a function parameter to the function (required) .
Each element of the array nums is an integer. When scanf() reads the integer entered by the user, it requires the address of the memory where it is stored. nums+i is the address of the i-th array element .

Array parameter definition

The following three methods are all possible, all need to pass the length of the array;

Neither int intArr[6] nor int intArr[] will create an array , and the compiler will not allocate memory for them. The actual array does not exist, and they will eventually be converted to int *intArr pointers. .

This form of int intArr[6] can only indicate that the function expects the array passed by the user to have 6 elements . It does not mean that the array can only have 6 elements. The actual array passed can have less or more than 6 elements.

It should be emphasized that no matter which method is used to pass the array, the length of the array cannot be obtained inside the function , because intArr is just a pointer, not a real array, so an additional parameter must be added to pass the length of the array.

//int max(int intArr[], int len){
    
    
//int max(int intArr[6], int len){
    
    
int max(int *intArr, int len) {
    
    
	int i, maxValue = intArr[0];  //假设第0个元素是最大值
	for (i = 1; i < len; i++) {
    
    
		if (maxValue < intArr[i]) {
    
    
			maxValue = intArr[i];
		}
	}
	return maxValue;
}

Function parameter passing is essentially a assignment process, the assignment is to copy the memory . The so-called memory copy refers to copying data from one memory to another memory.

For basic types of data like int, float, char, etc., the memory they occupy is often only a few bytes, and memory copying of them is very fast.

An array is a collection of a series of data. There is no limit to the number of data. It may be few or thousands. Copying them in memory may be a long process, which will seriously slow down the efficiency of the program. In order to prevent skills Poor programmers write inefficient code, and the C language does not syntactically support the direct assignment of data sets.

In addition to the C language, C++, Java, Python and other languages ​​also prohibit copying large blocks of memory, and use pointer-like methods to implement it at the bottom level.

Guess you like

Origin blog.csdn.net/qq_43641765/article/details/112449859