Chapter III Data
This chapter is very important to use, link properties, storage types within a specific scope. const, extern, and Statickeyword use. Almost all companies are the first issue of C + + in the interview.
Summarize:
Entities with external link properties (including external, internal, None) are referred to as global entities in terms of other languages, and all functions in CPP are able to access it.
Just declare it outside the function body or block of code (including the main function). The default link properties are external.
Assume that the declaration is inside a code block, stating that it refers to a global variable.
An entity with external link properties always has a static storage type (auto,static). Data of a static storage type is created before the program starts and always exists throughout the execution of the program. Local variables, in contrast, are created when the function executes.
Self-Active storage reasons: 1, reduce the total amount of memory.
2, the allocation on the stack can effectively solve the recursive problem.
Table scope, link properties, storage type Summary
Variable type |
Declaration Location |
The stack? |
Scope |
DECLARE static? |
Statement external? |
Global |
Outside the code block |
Whether |
Declare to end of document |
Restrict access to this document |
I feel external |
Local |
Code block starting at |
Is |
Entire code block |
Static storage |
becomes static storage |
Form participation |
function header |
Is |
The entire function |
Do not agree to exist |
Do not agree |
Warning:
1. Use easy misleading notation when declaring pointer variables.
Int *a, *b, *c;//good
int * a,b,c//Only A is a pointer to int
2, misunderstanding the meaning of initialization in the pointer declaration.
Char*message = "Hello world!" In fact, the assignment was given to the message, not *message.
Equivalent:
Char *message;
Message = "Hello world!"
3. Do not rely on implicit declarations, explicitly declaring a clearer expression of the intent of the program.
An enumeration type is best to explicitly define a value of an integral type, with an integer value of 0 for the default first character and a value of 1 greater than the value of the preceding symbol name. And so on
Enum Week{monday, Tuesday, Wednesday, Thursday, Friday, Saturday, sunday};//monday=0
Enum week{monday= 1, Tuesday = 2 ...};
4. When defining a new name for a type, use typedef instead of # define.
The advantage is obvious that when a # define is created to create a new type name, the latter fails to handle the pointer type correctly.
#defined_ptr_to_char char *
D_ptr_to_chara, B;
Just like int* A, B, it correctly declares a, but B is declared as a character.
5. Use const declaration to avoid variable changes.
Int Const *PTR; Declares a pointer to an integral type constant
Int *const ptr;//declares a pointer constant pointing to an integral type
Int const *const PTR; Declares a pointer constant that points to an integral type constant
6. Use name constants instead of literal constants
Example: #define Max_element 50
Intconst max_element = 50;
The former is a name constant that defines the length of the array. The latter is a literal constant and cannot define the length of the array.
7. Do not use the same variable name between nested blocks of code.
8. In addition to the detailed definition location of the entity, Externkeyword is used in its other declaration locations.
Question 15
Assuming that function a declares a automatic variable x, you are able to access X in other functions, simply by using the following declaration.
extern intx;
Right or wrong? Suppose X is declared static, right or wrong?
Answer: All wrong. Variables can only be interviewed at their own scope. The scope of X is his a function, which cannot be visited at other scopes.
Static only changes its storage type.
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
C and pointers (pointers on C)--Chapter III--Data