Const, typedef, define Summary

Source: Internet
Author: User
Const keyword
Const = read only, which is modified as a read-only variable rather than a constant. The variable modified by const cannot be used as the dimension of the array or placed after case: of the switch statement.
Main functions: 1. By modifying variables or parameters that do not want to be modified with const, the compiler will protect these variables from being modified to enhance system reliability; 2. Enhance code readability
[HTML]View plaincopyprint?
  1. Const int A; // A is a constant and cannot be modified.
  2. Int const A; // A is a constant and cannot be modified.
  3. Const int * A; // A is a pointer to a constant. The value of A can be variable.
  4. Int * const A; // const modifies a. A is a pointer constant pointing to an integer variable.
Initialization and assignment are required for definition. Once defined, the assignment cannot be modified. When the above Code 1, 2, and 4 are compiled, an error is reported indicating that the Code is not initialized.
If the const and volatile keywords follow the type specifiers, they act on the type specifiers; otherwise, they act on the pointer asterisks next to their left.

                                                                                                                                                                                                                                                                                                                    

The difference between typedef and define lies in two aspects: 1. Define can extend the defined macro type name, while typedef cannot. [CPP]View plaincopyprint?
  1. # Define peach int
  2. Unsigned Peach I; // No problem
  3. Typedef int Peach;
  4. Unsiged Peach I; // error, invalid
The define keyword is only a simple replacement of characters, while typedef can be regarded as an encapsulation of types and a new name is given for existing types.
2. In the declaration of several consecutive variables, only typedef can ensure type consistency. [CPP]View plaincopyprint?
  1. # Define int_ptr int *;
  2. Int_ptr chalk, cheese;
Chalk is of the int * type, while cheese is of the int type. Because define is just a simple string replacement
                                                                                                                                                                                         
Special usage of typedef

Used to declare function pointers

[HTML]View plaincopyprint?
  1. Typedef void (* ptr_fun) (INT );
  2. // Ptr_fun is a function pointer with the int type and the return value is void.
  3. Ptr_fun Sinal (INT, ptr_fun)
  4. // Sinal is a function that accepts two types of parameters: int and ptr_fun. The returned value is ptr_fun.

Typedef int A [5];

// A is an array pointer pointing to an integer array with five elements. A represents the (INT) [5] type. That is, a B = {1, 2, 3, 4, 5 };

Const, typedef, define Summary

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.