Constant pointers and pointer constants

Many posts on the Internet often say that constant pointers and pointer constants are stupidly indistinguishable. . . Chinese is too difficult to understand, and there are two different ways of saying it online . For example, whether pointer to const (pointer to constant) is called a constant pointer or a pointer constant, the usage of const pointer and pointer to const is correct, but the names are inconsistent, which leads to a lot of misunderstandings. The understanding of smearing adds inconvenience.

So I looked at the definition in English :

pointer to const    ----> const int *p       a pointer, which points to a const

const pointer     ------->int * const p    a pointer, which is const

This understanding is very easy, the definition clearly expresses the key of const modification, that is, a const modified pointer is a const pointer, otherwise it is a pointer to const. It will not be as confusing as constant pointers, pointer constants. Therefore, the following discussion is for const pointer and pointer to const.

Before talking about pointers, let's talk about constants. A constant is a quantity that cannot be changed, as opposed to a variable.

Let's see an example:

#include <iostream>
using namespace std;

intmain()
{
	int const t = 5;
	t = 9;
	return 0;
}

The result will of course report an error, because the constant cannot be changed, in other words, it is read-only. see error message


Of course, const int is the same as int const.

Then look at the first protagonist, pointer to const, the data type is int as an example const int *p (same as int const *p),

We know that *p represents the value of the address space pointed to by the pointer p, which is equivalent to the constant t in the above example in a sense; then since t is read-only, is it the same for *p?

#include <iostream>
using namespace std;

intmain()
{
	const int t = 5;
	t = 9;

	int a = 1;
	int b = 2;
	int c = 4;

	const int *p = &a;
	*p = 4;
	return 0;
}
The above t is a constant, p is a constant pointer, the analogy should report an error. As expected, p is also read-only.

So far, the first conclusion has been drawn : const int *p, *p cannot be changed, that is, the value pointed to the address space cannot be changed through a pointer like a normal pointer. Of course, whether the value of the address space itself changes or not has nothing to do with the pointer.

Of course, the constant pointer is not finished yet, and *p cannot be changed, so can p be changed?

#include <iostream>
using namespace std;

intmain()
{
	int a = 1;
	int b = 2;
	int c = 4;

	const int *p = &a;
	cout <<*p <<endl;
	p = &b;
	cout <<*p <<endl;
	return 0;
}
And look at the running results:


The result shows that there are no errors, and p successfully changes the point.

At this point, the second conclusion is drawn : const int * p, the pointer itself p can change the point.


Next, look at const pointer, take int type as an example, define int * const p

Here const directly modifies the pointer p (personal tendency is called constant pointer), which means that the point of p cannot be changed, see the example results

#include <iostream>
using namespace std;

intmain()
{
	int a = 1;
	int b = 2;
	int c = 4;
	int * const p = &a;
	p = &a;
	return 0;
}
The result is an error:


So far, the third conclusion is drawn : int * cosnt p, p itself is const and cannot be changed.

So is it possible to change the value of the address space pointed to by the pointer?

#include <iostream>
using namespace std;

intmain()
{
	int a = 1;
	int * const p = &a;
	*p = 10;
	cout <<*p <<endl;
	cout <<a <<endl;
	return 0;
}
The result is as follows:

Conclusion four : int * const p, the value of the address can be modified through the pointer.


But in fact, the key point is that, depending on who is directly modified by const, what follows const cannot be changed, that is, read-only.


English definition reference: http://blog.csdn.net/richardysteven/article/details/5981877

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324551803&siteId=291194637