To explain the introduction of pointer assignment in C language programming _c language

Source: Internet
Author: User
Tags constant modifier value of pi

Speaking from the const int i

You know when we declare a variable like this int i; This i is possible to reset the assignment at it. As follows:

int i = 0;
/* ... *
/i = 20;/* Here is a new assignment. * *

But one day my program might need such a variable (call it a variable) and assign an initial value at the time of declaration. After that, my program will not be able to assign it again at any other place. What should I do then? With a const.

/* ... * *
const int IC =20;
/*/IC = 40/* This is not possible, compile-time is unable to pass, because we can not reset the const modified IC. * *
* So our program will be easier to find the problem earlier. */
/* . . . */

With a const-modified IC We don't call it a variable, but a symbolic constant that represents the number 20. This is the role of the Const. The IC is not able to reassign the new value at its place.

After recognizing the role of the const, in addition, we also need to know the format of the wording. There are two kinds:

const int IC = 20;

And

int const IC = 20;

They are exactly the same. We have to be clear about this. In conclusion, it is important to remember that neither the const nor the int will have any effect on semantics before they are written. With this concept, let's look at these two guys:

const INT *PI

And

int const *PI

Do they have different semantics, according to normal logic? Oh, just remember one point: int and the const which before which put is the same, like const int IC; and int const IC; In other words, they are the same.

All right, we've got a "double tire" problem now. So

int *const pi;

What's the difference from the first two statements? Here is a detailed analysis of their format and semantics bar.
Let's say what the const int *PI is (and of course the Int const *PI is the same, as we said before, they're actually the same). Look at the following example:

#include "stdio.h"

Main ()
{/
  * code start/
  int i1 = =;
  int i2 =;
  /* Now the contents of pi variable is i1 memory address/
  const int *PI = &i1;
  * Note here, Pi can be at any time to reassign a new memory address////
  * pi variable content of i2 memory address * *
  pi = &i2;
  /* Think about this: can you use *PI = 80来 instead? Of course not! * *
  i2 =;
  printf ("%d\n", *PI); /* Output is * *///
  * Code END/*


Semantic Analysis:

I can see that the value of pi could be modified. That is, it can point back to another address, but you cannot modify the value of I2 by *pi. Does this rule fit the logic we talked about earlier? Of course it fits.

First of all, the const modifier is the entire *pi (note that I'm writing *pi instead of pi). So *pi is a constant and cannot be assigned (although Pi refers to i2 as a variable, not a constant).

Secondly, Pi is not modified by const, so pi is a pointer variable that can be assigned to another memory address. You may wonder: then how can I use const to modify PI? In fact, you notice that the position of the const in the int *const Pi will probably be understood. Keep in mind that semantics are seen through formatting. Haha, you may have seen the law, right? There is no need to see the next section. But I still have to go on with my fight.

Look again int *const pi

Indeed, the int *const pi is easily confused with the preceding int const *PI. Note: The const in the preceding sentence is written before and after Pi, not in the *PI. Obviously, it is modified to qualify pi. I'll show you the example first:

#include "stdio.h"

Main ()
{/
  * code start/
  int i1 = =;
  int i2 =;
  int *const pi = &i1;
  /* pi = &i2; Note here that pi can no longer be assigned this way, that is, you cannot point to another new address. * *
  * So I have commented on it. * *
  I1 = 80/* Think about it: can you use *PI = 80 instead? OK, here you can modify the I1 value by *PI. (Comment on line 5th)/////
  * Please compare yourself with the previous example.
  /printf ("%d", *PI)//* Output is * *///
  * Code END * *


Semantic Analysis:

Look at this code, what do you understand? There was no value in the PI value that could be modified. It can always point to the memory address that was initialized. Instead, you can modify the I1 value by *pi this time. Compare it to the previous example and look at the following two points analysis:

PI has a const modifier, so it's just a pointer constant: that is, the PI value is not modifiable (that is, Pi cannot point back to the i2 variable) (see note on line 4th).
There is no const modification to the front of the whole *pi. In other words, *PI is a variable rather than a constant, so we can modify the value of the memory I1 it refers to by *PI (see the comment on line 5th).
In a word, this time pi is a pointer constant that points to the int variable type data.

I conclude by concluding two sentences:

If the const modifier is *PI, it cannot be changed to *PI (that is, it cannot be like this: *pi=50; assignment) rather than PI.
If the const is written directly before Pi, then pi cannot be changed (that is, it cannot be:pi=&i; assignment like this).
Please be sure to remember these two points first, I believe you will not be again by them to get the paste. Now look at these two statement statements int const *PI and int *const pi when, oh, you will be dizzy or very relaxed and comfortable? What can be modified by their respective stated pi, and what cannot be modified? Consider these questions again.

Three additional cases

Here, I add the following three kinds of cases. In fact, as long as the above semantics are clear, these three kinds of situations are already included. However, as three specific forms, I would simply mention it.

Case one: int *pi pointer pointing to const int I constant

#include "stdio.h"

Main ()
{/
  * * Begin/
  const int i1 =;
  int *pi;
  PI = &i1; /* Is this OK? No, VC under the compiler is wrong. *//////
  * The I1 address of the const int type cannot be assigned to a pointer pi that points to an int type address. Otherwise PI will be able to modify the value of I1 it! *
  /PI = (int *) &i1;//* Is this OK? Coercion type conversions are supported by C. * *
  * VC compiler passed, but still can not through *PI = 80来 Modify the I1 value. Go and try it! Look at the specifics. * *////* End/
}

Case two: the const int *PI pointer points to the const int I1

#include "stdio.h"

Main ()
{/
  * Begin/
  const int i1=40;
  const int * PI;
  pi=&i1;/* two types are the same, you can assign values like this. Obviously, the value of I1 can not be modified either by pi or by I1. * *////* End/
}

Case three: A pointer with a const int *const PI Declaration

#include "stdio.h"

Main ()
{//
  * Begin/
  int i;
  const INT * Const pi=&i; * Can you imagine what pi can do? The pi value cannot be changed, nor can the value of I be modified by PI. Because whether it is *PI or pi are const. * *////* End/
}

Related Article

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.