An introductory example of pointer assignment in C programming

  • 2020-05-07 20:05:48
  • OfStack

starts with const int i

You know when we declare a variable like this int i; This i is likely to be re-evaluated at it. As follows:


int i = 0;
/* . . . */
i = 20; /* It's reassigned here */

But one day my program might need a variable (let's call it a variable) that has an initial value when it's declared. And then my program won't re-assign it anywhere else. So what should I do? Use const.


/* . . . */
const int ic =20;
/* . . . */
ic = 40; /* This is not possible, it won't pass at compile time, because we can't const  Modification of the ic Reassigned. */
/* This way our program will find the problem earlier and easier. */
/* . . . */

With const we don't call it a variable, we call it a symbolic constant, which is the number 20. That's what const does. ic cannot be reassigned to a new value.

Now that we know what const does, we also need to know how to write the format. There are two:


const int ic = 20;

with


int const ic = 20;

They're exactly the same. We need to be clear about this point. In short, it is important to remember that neither const nor int affects the semantics. With this concept in mind, let's look at these two guys:


const int *pi

with


int const *pi

In ordinary logic, do they have different semantics? Hehe, as long as remember 1 point: int and const which put before which put after are 1 kind of, just like const int ic; With int const ic; 1 sample. In other words, they are the same.

All right, we've got one "double tire" problem now. then


int *const pi;

What's the difference between these two statements? The following is a detailed analysis of their format and semantics.
First let's talk about what const int *pi does (of course int const *pi is also 1, as we said before, they are actually 1). Here's an example:


#include "stdio.h"

main()
{
  /*  The code begins to  */
  int i1 = 30;
  int i2 = 40;
  /*  now  pi  The content of the variable is  i1  Memory address  */
  const int *pi = &i1;
  /*  Notice here, pi You can reassign at any time 1 Three new memory addresses */
  /* pi  The content of the variable is  i2  Memory address  */
  pi = &i2;
  /*  Think about it: it works here *pi = 80 Instead? Of course not! */
  i2 = 80;
  printf("%d\n", *pi); /*  The output is 80 */
  /*  End of the code  */
}

Semantic analysis:

See, the value of pi can be modified. That is, it can be redirected to another address, but the value of i2 cannot be modified by *pi. Does this rule follow the logic we talked about earlier? Of course it does.

The first const modifies the entire *pi (note that I wrote *pi instead of pi). So *pi is constant and cannot be assigned (although pi refers to i2 as a variable, not a constant).

Second, pi is not decorated with const, so pi is a pointer variable that can be assigned to another memory address. You may be wondering: how do I modify pi with const? In fact, if you notice the location of const in int *const pi, you'll get the idea. Remember, look at the semantics in terms of format. Haha, you probably see the pattern already? There's no need to watch the next one. But I have to keep fighting.

, int *const pi

Indeed, int *const pi is easily confused with int const *pi. Note: the const in the first sentence is written before pi and after the * sign, not before *pi. Obviously, it qualifies pi. Let me show you an example:


#include "stdio.h"

main()
{
  /*  The code begins to  */
  int i1 = 30;
  int i2 = 40;
  int *const pi = &i1;
  /* pi = &i2;  Notice here, pi You can't do that anymore, you can't point to anything else 1 A new address. */
  /*  So I've annotated it. */
  i1 = 80; /*  Think about it: it works here  *pi = 80;  Instead? Yes, we can go through here *pi Modify the i1 The value of the. (the first 5 Line comment) */
  /*  Please move to the front 1 Let me give you an example.  */
  printf("%d", *pi); /*  The output is 80 */
  /*  End of the code  */
}

Semantic analysis:

What do you understand about this code? Did you find that the pi value cannot be re-assigned and modified? It can only always point to the memory address at the time of initialization. Instead, this time you can change the value of i1 by *pi. Compare with the previous example, let's look at the following two analyses:

Because of the const modification, pi is just a pointer constant: that is, the pi value is unmodifiable (that is, pi cannot point back to the variable i2) (see the comment on line 4).
The entire *pi is not preceded by const. That is, *pi is a variable, not a constant, so we can modify the value of i1 in memory by *pi (see note on line 5).
All in all, this time pi is a pointer constant to data of type int.

I conclude with two sentences:

If const is before *pi, what cannot be changed is *pi(i.e., it cannot be like this: *pi=50; Assignment) instead of pi.
If const is written directly before pi, pi cannot be changed. The assignment).
Please remember these two points first, I believe you will not be confused by them. Now if you look at these two statements int const *pi and int *const pi, would you be dazed or relaxed? What can and cannot be modified by their respective declared pi? Think about these questions again.

complements in three cases

Here, I add the following three cases. In fact, as long as the above semantics are clear, the three cases have been included. But let me just mention it briefly as one of the three concrete forms.

Case 1: int *pi pointer to const int i constant


#include "stdio.h"

main()
{
  /* begin */
  const int i1 = 40;
  int *pi;
  pi = &i1; /*  Is that ok? No, VC This is a compilation error. */
  /* const int  The type of i1 The address cannot be assigned to the point int  A pointer to a type address pi . Otherwise, pi Is it possible to modify i1 Is it worth! */
  pi = (int *) &i1; /*  Is that ok? Casting can C Supported. */
  /* VC The following compilation passed, but still failed  *pi = 80 To modify the i1 The value of the. Try it! Let's see how that works. */
  /* end */
}

Case 2: const int *pi pointer to const int i1


/* . . . */
const int ic =20;
/* . . . */
ic = 40; /* This is not possible, it won't pass at compile time, because we can't const  Modification of the ic Reassigned. */
/* This way our program will find the problem earlier and easier. */
/* . . . */
0

Case 3: pointer declared with const int *const pi


/* . . . */
const int ic =20;
/* . . . */
ic = 40; /* This is not possible, it won't pass at compile time, because we can't const  Modification of the ic Reassigned. */
/* This way our program will find the problem earlier and easier. */
/* . . . */
1


Related articles: