Summarizing the use of the Const keyword in C language _c language

Source: Internet
Author: User

What is a const?

A constant type refers to a type that is described using the type modifier const, and the value of a variable or object of a constant type cannot be updated. (Of course, we can update cynical:)

Why the introduction of const?

The initial purpose of the const rollout is to replace the precompiled instruction, to eliminate its drawbacks, and to inherit its advantages.

The Const keyword is very flexible to use, this is very different from PHP, the const in PHP is used to define a constant in the class, and in C, the const because of different position has different role, because the situation has different roles, use is very flexible.

(1): const is used to modify ordinary variables (except pointer variables), const type name and type const name the two forms are completely equivalent, all indicate that they are constants, can not be modified.

#include <stdio.h>
int main () {
  const int num =23;
  printf ("result=%d\n", num);
  Num =31;
  printf ("result=%d\n", num); Error, num is constant, cannot modify
}

(2): When the const is used to modify the pointer variable, it is divided into the following four kinds of situations

1. const type *name : In this case, the type object that the const-decorated pointer variable name points to, that is, the object being pointed to cannot be modified because it is a constant and the pointer variable can indeed be modified

#include <stdio.h>
int main () {
  int tmp = n;
  const int *num = &tmp;
  printf ("result=%d\n", *num);   
  (*num) = 24; Error, because pointer num points to an object of type int that is not modifiable
  ("result=%d\n", *num); 

2 type const *name . In this case, the type object that the const-decorated pointer variable name points to is exactly the same as the same, but reverses the following order.

#include <stdio.h>
int main () {
  int tmp = n;
  int const* num = &tmp;
  printf ("result=%d\n", *num);   
  (*num) = 24; Error, because pointer num points to an object of type int that is not modifiable
  ("result=%d\n", *num); 

3. type * const name : In this case, the const-decorated pointer variable name means that the value of the pointer variable cannot be modified, but the object pointed to by the pointer variable can indeed be modified

#include <stdio.h>
int main () {
  int tmp = m;
  int *const num = &tmp;
  printf ("result=%d\n", *num);  
  int change = n; 
  num = &change; Error, because pointer num is
  printf ("result=%d\n", *num) that cannot be modified; 

4. const type * const name : In this case, the const-decorated pointer variable name and the object that the pointer variable name points to, that is, the pointer variable and the object to which the pointer variable is directed cannot be modified.

(3): The role of Const in the function of the parameter:

void get_value (const int num) {
  num=23;//Error
}

When calling a get_value() function, pass the NUM argument to the function, because the const is defined, so NUM cannot be modified in the function

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, if there is doubt you can message exchange, thank you for the cloud Habitat Community support.

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.