Summary of const with Pointers in C language

  • 2020-05-30 20:50:25
  • OfStack

Summary of const with Pointers in C language

Here to share 1 of their own experience, hope to share with you 1 technology, if you have any shortcomings, please correct. Writing this article, I hope you can grow up together. I also believe that there is no high or low technology, only complementary, only sharing, to make each other more growth.

Conclusion:

* the const value cannot be changed; the point can be changed The value of const * can be changed None of const * const can be changed

Example code:


#include <stdio.h>

int main(int argc, const char * argv[])
{

  // 1  Modifiable pointer 
  const int a = 10;
  int *p = &a;
  *p = 1000;
  printf("*p = %d\n", *p);

  // 2  Modifiable pointer 
  const b = 10;
  int *pb = &b;
  pb = p;
  printf("*pb = %d\n", *pb);

  // 3
  const c = 10;
  int * const pc = &c;
  *pc = 1000;
  //pc = pb; Can't change 

  //4
  const d = 10;
  const * int const pd = &d;
  //*pd = 1000;  Can't change 


  printf("\n");
  return 0;
}

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: