C Basic knowledge----easy to get headaches----Const && typedef && Define

Source: Internet
Author: User

Const keyword
Const=read only, modified as read-only variables instead of constants. A const-modified variable cannot be used as the dimension of an array and cannot be placed after the case of the switch statement:
The main functions are:1. By modifying variables or parameters that do not wish to be modified with const, the compiler will protect these variables from being modified to enhance the reliability of the system;2. Enhance the readability of your code
[HTML]View Plaincopyprint?
    1. const int A; A is a constant cannot be modified
    2. int const A; A is a constant 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; The const modifier A,a as a pointer constant to an integer variable
The definition must be initialized for assignment, which cannot be modified once defined. The above code 1, 2, 4 will be compiled with an error indicating uninitialized.
The const and volatile keywords are followed by the type specifier, which acts on the type specifier and, in other cases, the asterisk of the pointer immediately to the left of it.

                                                                                                                                                                                                                                                                                                                                    

the difference between typedef and define is embodied in two aspects1.define can extend the name of a defined macro type, and a typedef can not [CPP]View Plaincopyprint?
    1. #define PEACH int   
    2. unsigned peach i; //No problem   
    3. typedef   int Peach;
    4. Unsiged Peach i; //error, illegal   
The define keyword is just a simple character substitution, and a typedef can be seen as a wrapper over the type, with a new name for the existing type.
2 in the declaration of several consecutive variables only the TypeDef can guarantee the consistency of the type [CPP]View Plaincopyprint?
    1. #define INT_PTR int *;   
    2. INT_PTR Chalk,cheese;
Chalk is an int * type, while cheese is an int type. Because define is just a simple string substitution
                                                                                                                                                                                                  
Special use of typedef

Used to declare a function pointer

[HTML]View Plaincopyprint?
    1. typedef void (*PTR_FUN) (int);
    2. Ptr_fun is a function pointer with a parameter of type int and a return value of void
    3. Ptr_fun sinal (Int,ptr_fun)
    4. Sinal is a function that accepts arguments of int and ptr_fun two types, with a return value of Ptr_fun

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.