Differences between constant pointers and pointer constants in C Language

Source: Internet
Author: User

Differences between constant pointers and pointer constants in C Language

This article mainly introduces the differences between constant pointers and pointer constants in C language. If you need them, refer to them.

A constant pointer refers to a pointer to a constant. As the name suggests, a pointer points to a constant, that is, it cannot point to a variable, and its content cannot be changed, the pointer cannot be used to modify the content it points to. However, the pointer itself is not a constant, and its own value can be changed to point to another constant.

A pointer constant is a constant. The address it points to cannot be changed, but the content in the address can be changed through the pointer. The address it points to will be in its lifetime until the end of its lifecycle. Note that the pointer constant must be assigned the initial value at the same time during definition.

Note: Some people think in turn the definitions and meanings of these two names: "pointer constant: as the name suggests, its central word is" constant ", which is the focus, and pointer is a modifier. So the pointer here is still a variable, and its content stores the constant address. Constant pointer: the key word is a pointer, which cannot be changed. Because the pointer always points to an address, it means that the address it points to cannot be changed ". But I personally think the latter is unreasonable, so I use the former.

2. Usage:

Differences in usage: constant pointer: const pointer constant before *: const after.

Of course, we can also define constant pointer constants, so we need to add two const, one before and one after! The above only shows the essential differences between the two in terms of definition. There are still many changes in the specific use, but the things keep changing. We can analyze the essence of various complex usage based on its principles.

3. Example

3.1 constant pointer usage:

Such as intb, c;

Int const *;

A = & B;

A = & c;

Yes, but the memory it points to cannot be modified. For example, * a = 20; this is illegal! Error!

3.2 usage of pointer Constants

Such as Inta;

Int aTest;

Int * const p = &;

P is a constant pointer that points to the memory of variable. Pointer constants cannot point to other variables with p, such as p = & aTest; error! You can modify the value pointing to the memory, for example, * p = 20. The initial value must be assigned to me when the pointer constant is declared.

The pointer constant cannot be released, and p points to NULL, that is

P = NULL;

An error will be reported during compilation.

/Opt/test. c: 649: error: assignment of read-only variable 'P'

There is also a skill to remember their different forms! View the const keyword. The key following it cannot be modified. For example, if int * const a = & B; is followed by a, it means a cannot be modified!

Intconst * a = & B; * a indicates * a cannot be modified!

In many books or MSDN, constint a = 5; int B = 6; const int * p = & B;

In fact, constint * is the same as intconst *, that is, the constant pointer, that is, the data it points to (in this case, int) is a constant, and its own data type is constint *

There is also constint * p = & B; yes, although B is not a constant. But constint a = 6; int * p = & a; an error is reported because it eliminates the const attribute of.

4. Tips

Using pointer constants can increase code reliability and execution efficiency.

Such as Inta;

Int * const p = &;

Increased reliability: Do not worry about unexpected results caused by p modification or release;

Increase execution efficiency: p is not used as a null check in the subfunction to Improve the efficiency.

 

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.