03-const static extern

Source: Internet
Author: User
Tags array definition

Const is a C-language keyword that restricts a variable from being allowed to be changed. The use of const can improve the security and reliability of the program to a certain extent, and it can also help to understand the role of Const clearly when looking at other people's code works.

1. The difference between const and # define

(1) The compiler does not handle the same way

Define defined macros are expanded during the preprocessing phase, while const-defined constants are used during the compilation phase

(2) Different types and security checks

Define macros have no type, and do not do any type checking, just expand

Const constants have specific types that perform type checking during the compilation phase

(3) different storage methods

Define defined macro substitution is done before compilation, and how many places are used, how many temporary constants of memory are present, and no memory is allocated

Const constants are allocated in memory

2. Const variable VS constant

constants, such as 5 and "ABC", are definitely read-only, because constants are read-only areas that are placed in memory by the compiler, which of course cannot be modified. The read-only variable, in memory, opens up a place to store its value, except that the value is not allowed to be modified. The C keyword Const is a modifier that defines a variable that is not allowed to be changed.

Const int   5; int

In the above example, although the variable n is modified to a read-only variable, but not a constant, while the array definition, the specified array length must be constant, so will be an error. In other words, constants are not equal to immutable variables

3, const to limit the content

1typedefChar*pStr; 2 Char string[4] ="ABC"; 3 Const Char*P1 =string;//1-Type4 ConstPSTR P2 =string;//2-Type5p1++; 6p2++;

The basic form of const usage is:

const Type m; // Limit m immutable. 

In the 3rd line of code, the keyword modifier is *p1, so *p1 is immutable, but P1 is mutable, so p1++ is no problem

In the 4th line of code, the const modifier is P2, so P2 is immutable, which means that the 6th line of code is wrong.

4. Const VS pointer

1) const in front

1 Const int // Nvalue is a const 2  Const Char // *pcontent is const, pcontent variable 3  Const Char const pcontent;  // Pcontent and *pcontent are both const .

2) const in the back

 1  int   const  nvalue; //  Nvalue is a const  2    Const  * pcontent; //  *pcontent is const, pcontent variable  3  char  *  Pcontent; //  pcontent is const,*pcontent variable  4    Const  * const  pcontent; //  Pcontent and *pcontent are const  

1) and 2) are the same as the effect of the Declaration

Example: 

int Const * P1,P2;

Here P2 is const-Modified, and (*P1) is received as a whole const-modified. In other words, the P1 is mutable. In this example, P1 is a pointer to the shaping,

--the variable that the pointer points to is immutable, but pointing can change

 1  int  x = 1  ;  2  int  y =  ;  3    int  * px = &x; int  const  * px = &x; //  4  px = &y; //  correct, allow to change the pointer to  5  *px = 3 ; //  error, not allowed to change the value of the variable pointed to by the pointer  

--The value of the variable pointed to by the pointer can be changed, pointing to the invariant

  

1 int 1 ; 2  int 2 ; 3  int const px = &x; 4 // error, not allowed to change pointer pointing 5  3 // correct, allows changing the value of the variable pointed to by the pointer    

--the variable that the pointer is pointing to is immutable, pointing to non-mutable

int 1   ;    int2;   constintconst px = &x; int Const const px = &// error, not allowed to change pointer pointing to 3// error, Changing the value of the variable pointed to by the pointer is not allowed

5, when defining a macro, you can also use the const to define

When defining macros, you can also use const to define macros, but be aware of some issues. For example, if you put a const-modified macro in the PCH file, it will cause this statement in all the. m files in the project, creating the problem of rereading the definition, and the solution is to re-create a new one. h and. m files, and then define constants in. m, and then use the extern keyword in. h to reference it.

For example:

--Define the following in the. m file:

1NSString *ConstHwappkey =@"3235932662";2NSString *ConstHwredirecturi =@"http://www.baidu.com";3NSString *ConstHwappsecret =@"227141af66d895d0dd8baca62f73b700";4NSString *ConstHwmyage =@" -";

--Reference with the extern keyword in the. h file

1 extern Const Hwappkey; 2 extern Const Hwredirecturi; 3 extern Const Hwappsecret; 4 extern Const Hwmyage;

When using the global variables of these declarations, simply include the header file of the file.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {StaticNSString *id =@"Cell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:id]; if(!cell) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id]; } Cell.textLabel.text= [NSString stringWithFormat:@"Row%ld Data", Indexpath.row]; returncell; }

The above notation is often used when using Tableviewcell, and a static variable is defined that initializes the variable at compile time, meaning that the variable is either nil or is assigned at the beginning of the definition, in general, Only NSString or basic types are used.

Static modified variables are stored in an in-memory quiescent store, which exists during the run of the program within the block. Therefore, the uniqueness and persistence of static variables are guaranteed.

--static variables can not be written in interface, will be directly error. The static variable can only be placed inside the method or outside the implementation (usually at the beginning of the implementation file),

#import " Printer.h " Static int PageCount, @implemenation Printer ... @end

In simple terms, const is a static variable that exists in memory in a special place after the declaration, and the next time it is used, the last stored value is read from that memory.

Reference:

Http://blog.163.com/[email protected]/blog/static/13174582620139319928864/

03-const static extern

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.