C++ pointers in detail, the connection and difference between pointers and arrays

Before introducing the concept of pointers, you need to understand three basic attributes that must be tracked when a computer program stores data:

  • Where is the information stored, that is, the address;
  • What is the stored value, referred to as value;
  • What type of information is stored, referred to as type;

1, the address operator &

When conventional variable definition, look for the variable value and type very convenient if you want to know the address of the variable is stored (ie, memory area), then you need to use the address operator &

int main()
{
	int a = 3;
	int b = 4;


	cout << "变量 a 的地址为 " << (&a) << endl;
	cout << "变量 b 的地址为 " << (&b) << endl;
	
	return 0;
}

Two integer variables a, b, are defined here; the & address operator is added afterwards, and the storage addresses of the variables a and b are printed out on the debugging console respectively;

image-20210129224319817

From the above results, it can be seen that the hexadecimal notation is used when displaying the address, and the difference between the variables a and b is 4 (that is, four bytes), because both a and b are of int type;

2. Definition and usage of pointers

A pointer is a special type of variable used to store the address of a value . The pointer name is the address. The ***** operator is called an indirect value or contact reference operator. This operator is applied to a pointer, and you get The value stored at this address;

In the following example, I use int * to declare a pointer variable p and an int variable b (copy to 4); and p point to b

  • Variable addresses can be expressed in two ways, p and &b;

  • Variable values ​​are also expressed in two ways, *p and b;

int main()
{
	int* p;//定义一个指针p 类型为 int;
	int b = 4;
	
	p = &b;//将 p 指向 b;

	cout << "变量 b 的地址为 " << p << endl;
	cout << "变量 b 的地址为 " << (&b) << endl;
	cout << "变量 b 的值为" << b << endl;
	cout << "变量 b 的值为" << *p << endl; // *p 表示指针p中存储的值;
	
	return 0;
}

The results are as follows:

image-20210129231004390

The way to declare a pointer is as follows, where typename can be replaced with int, char, double, indicating the type pointed to by the pointer

typename* p;// typename 表示类型,p表示变量;

It can also be initialized at the same time as the declaration. It should be noted that the pointer is initialized, not the value pointed to by the pointer;

int a =4;
int *pt =&a;

There is a big difference between using a pointer and using a regular variable: when using a regular variable, the value is a specified quantity, and the address is a derived quantity. In the pointer strategy, the address is the specified amount, and the value is the derived amount;

Incorrect use of pointers

When C++ creates a pointer, the computer will allocate the memory used to store the address, but will not allocate the memory used to store the data pointed to by the pointer. The space allocated for the data is an independent space and cannot be omitted , as follows

int *pt;//定义一个指向 int 类型的指针 pt;
*pt = 23;// 错误,指针 pt 未指向任何地址,

In the above example, I want to assign the value pointed to by the pointer pt to 23, but the pt here is not initialized and does not point to any value, which means we don’t know where pt points to; if 23 is directly assigned to *pt, it may cause Some of the most secretive and difficult to track bugs;

So you need to keep in mind that before applying the dereference operator (*) to the pointer, initialize the pointer to a certain, appropriate address

3. Pointers and arrays

In C and C++, pointers and arrays are basically equivalent because of pointer arithmetic and the way C++ internally handles arrays;

  • Normal integer variable plus 1, then the value plus 1;
  • The pointer variable plus 1 is equal to the original address value plus the total number of bytes occupied by the pointing object (for example, double type plus one needs to increase 8 bytes). According to the change of the pointing address, *p (the value pointed to by the pointer) Also changes

The relationship between pointer and array

  • When assigning an array name arr to a pointer p int *p = arr(the type is the same), it means that the pointer p points to the address arr[0] where the first element of the array is locatedp = arr[0]
  • When the pointer is increased by 1, and the value pointed to by the pointer is the value where the original index of the array is increased by 1 *(p+1) == arr[n+1],

The example is as follows, the pointers p and q point to the arrays arr[0] and arr respectively when they are initialized; when p and q are increased by 1, the values ​​they point to have not changed;

int main()
{
	int* p;//定义一个指针p 类型为 int;
	int *q;
	int b[2] = {3,4};

	p = &b[0];//将 p 指向 b;
	q = b;// 指针 q 指向数组 b 的第一个元素

	cout << "变量 p 的地址为 " << p << endl;
	cout << "指针变量 p 指向的值为 " << *p << endl;
	cout << endl;
	cout << "指针变量 q 指向的值为 " << *q << endl;
	
	cout << endl;
	cout << endl;
	cout << "变量 p+1 的地址为" << p+1 << endl;
	cout << "指针变量 p+1 指向的值为" << *(p+1) << endl; // *p 表示指针p中存储的值;
	cout << endl;
	cout << "指针变量 q+1 指向的值为" << *(q + 1) << endl; // *p 表示指针p中存储的值;
	
	return 0;
}

image-20210130003713751

When array represents an array, sizeof(array) can test the size of the array, in bytes;

When array is an array, array + n means a pointer to the array index n-1, use sizeof(array +n) to point to the size of the pointer type;

int main()
{
	int* p;//定义一个指针p 类型为 int;
	int *q;
	int b[4] = {3,4,5,6};

	p = &b[0];//将 p 指向 b;
	q = b;// 指针 q 指向数组 b 的第一个元素


	cout << "数组 b 的大小为 " << sizeof(b) << endl;
	cout << "指针 b[1] 的大小 " << sizeof(b+1) << endl;
	cout << "指针 p+1 的大小 " << sizeof(p+1) << endl;
	
	return 0;
}

The results are as follows

image-20210130005832064

Guess you like

Origin blog.csdn.net/weixin_42512684/article/details/113458318