Some collation of pointer constants and constant pointers

Source: Internet
Author: User

Recently learning pointers, encountering a problem with pointer constants and constant pointers, was previously made into a dilute paste

Tu, today specially check the data collated a bit, now take out and share with you, there is anything wrong place please

We are very generous to enlighten, common progress.

First, the constant pointer, we can understand that the constant pointer is a pointer to a constant, since the point

To a constant, the value that it points to is, of course, not changed. But the value of it itself, i.e. its ground

The address can be changed, and we can point it at a different location.

And the pointer constant, it can be understood as a constant, when constant?? Pointer constants, that is, pointers are often

, the address is immutable, but the value of the address it points to is variable.

A constant pointer of type int is defined like this: int const* the pointer name. int const (constant) * (

pointer) pointer name
A pointer constant of type int is defined like this: the int* const pointer name. int* (pointer) const (often

Volume) pointer name
Observe the above two definitions we can judge by the position of * and Const is a pointer

Constant or a constant pointer??? * Before const is a pointer constant, const before * is a constant pointer!!

! Oh, of course, the most important thing is to understand the meaning they represent, which can change, which can not!

Now it's time to look at the following code to see how we can understand. Try the following code

Where is the problem, why!

#include <stdio.h>
int main () {
int i=0;
int j=1;
int x;
const INT * pi=&i;
*pi=22;
pi=&j;
int * Const pj=&j;
*pj=22;
pj=&i;
const INT * px=&x;
const int h=2;
Const int* ph=&h;
int* Const ph2=&h;
}

Understand:

#include <stdio.h>
int main () {
int i=0;
int j=1;
int x;
The const int * pi=&i;//defines a constant pointer to pi, which points to the address I
*pi=22;//has a problem, pi is a constant pointer, it points to the value of the address you dare to change
pi=&j;//no problem, although Pi point is a constant, but itself is still free, or can be changed

Change the
int * Const PJ=&AMP;J;//defines a pointer constant here, pointing to the address of J.
*pj=22;//no problem, the pointer constant is just a constant, the address is constant, but the content inside of me

Can change ah, at least a little free ah
pj=&i;//here is wrong, since it's a constant, just don't change the address.
const INT * px=&x;//There is no problem here, maybe someone will have doubts, you px is not a

Constant pointer, don't you say constant pointer is a pointer to a constant, x is not a constant, miscellaneous

You can do it? Oh, this is my previous question, and then I put the PX point to the value of the output, and then

Trying to assign a value to PX, but no, X has been assigned a garbage value.
So the constant pointer is a pointer to a constant, which is just so easy for us to understand, in fact it's still

Can point to a variable, but once defined, the variable becomes a constant amount.
const int h=2;
int* Const ph2=&h;//There is a problem, I defined above h is a constant, how can you use a

Pointer variable to point to H. If you can, your pointer is not always able to modify the contents of the address you are pointing to.

That doesn't mean you can change the value of H, but H is a constant, how can you do that!!!
Const int* ph=&h;//So use a constant pointer, the value of the address is not changed.
}

In fact, I think the Chinese translation is more mysterious.

What you call a constant pointer, in fact English is pointer to const, often translated achievement called pointer to constant. In fact, it's not so easy to confuse.
In addition, the pointer constant, English is a const pointer, often can see translated into regular pointers, if so called but feel easy and the above that confused.

And about the word constant, as a term, there is a certain ambiguity. Sometimes it only contains things like 1, 1L, "abc", 12.5 ' C '. Sometimes he also refers to the const variable. It means something like a const int, declaring a variable, but as a constant. (It is common to translate "constant variables" or "constants", but according to your naming logic it should be called "Variable Constants"). And most of the time in the context of actually referring to the latter only, because the former is relatively simple, usually do not need to consider.

Later, the standard committee changed a word, using literal to represent 1, ' C ' such things, are generally translated into "literal." For example, the former is called Plastic literal, the latter is called character literal. constants, such as const int A, are said to declare a const integer. But Chinese do not know is not a habit problem, also has the word literal to translate into literal constants.

In short, the translation is still very confusing. But the floor of the speaker is still clear, as long as not to die to pick the two Chinese terms on the line.

In addition, I have seen another way of remembering:
const int * const P;
As a const int (* const p). The understanding of the former as the addition of the things after the *, the parentheses in the understanding of non-star things.
When present such as: *p = 5; When such a statement, because of the star, then to look outside the parentheses, the left thing is essentially a const int.
and P = 5; If you look for a const p in parentheses, this is a constant assignment.

This notation is also useful for complex things:

Program code:intF () {return 0; }

intMain ()
{
int(*p) (void);
p = &f;

inta[5];
int(*Q) [5];
Q = &a;

return 0;
}

Int (*p) (void). P is a pointer, and *p is an int _ (void), so it is a pointer to a function.
int (*Q) [5]. Q is a pointer, and *Q is an int [5], so q is a pointer to an array such as int [5].
Of course, complex things can be complicated, regardless of how they are written.

Some collation of pointer constants and constant pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.