A question in C: pointer constants and constant pointers

Source: Internet
Author: User

The original question isCodeThen, ask if an error is reported and what will be output.

This is also a simple pen question for a friend to join in school recruitment.

Just a few weeks ago, I was bored with reading this C Programming getting started book.

Let's look at the scope of the variable.

1. Static variables are initialized only once.

This is a better understanding of putting variables in the global environment.

But for example, the method

Void scope_test ()

{

Static int A = 0;

Static int B;

B = 1;

Printf ("A = % d and a address % P -- B = % d and B address % P \ n", a ++, & A, B ++, & B );

}

Because we have read about declarative variables and initialization variables before.

Int A = 2; // declare and initialize a = 2;

And int 2; A = 2; // same as above

It is equivalent.

However, after testing through the above method, it is found that the output is

IP addresses a = 0 and a 0x10d27e090 -- B = 1 and B 0x10d27e094

IP addresses a = 1 and a 0x10d27e090 -- B = 1 and B 0x10d27e094

Address 0x10d27e090 for a = 2 and a -- Address 0x10d27e090 for B = 1 and address 0x10d27e094 for B

IP addresses a = 3 and a 0x10d27e090 -- B = 1 and B 0x10d27e094

IP addresses a = 4 and a 0x10d27e090 -- B = 1 and B 0x10d27e094

(This method is executed multiple times during testing)

We can see through log that changes to the value of variable A of the static type are in line with our understanding.

But for B, it is always 1.

Then I figured out that int B and B = 1 are actually two statements.

C LanguageIf int B is declared but the initialization operation is not followed by the current statement, it is automatically initialized to 0.

The second statement B = 1 is actually a value assignment process.

So when we see the above output, we can see through the address output that not B is initialized again, but every call is re-assigned.

Only 1.

2. pointer constants and pointers to constants.

Const int * P and int * const P;

The former indicates a pointer to a constant. We cannot use a pointer to change the value of the variable pointed to by the pointer.

For example

Int A = 3;

Const int * B;

B = &;

(* B) = 4; // The Compiler reports an error, prompting that A is read-only.

However, we can assign a value to B.

Int c = 9;

B = & C;

The latter indicates that the pointer P is a constant, which cannot be changed after P is initialized.

Int A = 3;

Int * const B = &;

(* B) = 4;

A small problem is mentioned here.

If you write

Int * const B;

B = &;

The same error is reported. This explanation is the same as the declaration and initialization mentioned above.

After the statement is declared, it is automatically initialized. The second statement executes a secondary value assignment, and constants cannot be assigned repeatedly.

As mentioned in the book, the declaration of a constant must assign values to its initialization. If you want to assign values to it later,

However, if the system has initialized it, it will not be able to perform the value assignment operation you want, and the constant definition will lose the meaning you want to express.

Of course, there is also an abnormal Representation

Const int * const P; Fixed cannot be changed.

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.