The difference between c-const and static, pointers as function return values, pointers to functions, enumeration

Source: Internet
Author: User

The difference between const and static const--read-only, used only in declarations
1 Const int Ten ; 2 int Const ten;
Num variable is read-only and cannot be modified
1 Const int arr[4] = {ten ) ; 2 int Const arr[4] = {ten , +} ;
The value of an array's elements cannot be modified, read-only
1 Const int *p1 = # 2 int const *P1 = #
The value of the variable pointed to by the pointer cannot be modified by the P1 pointer, but if the direct manipulation of the variable is possible, the value of the pointer variable can be changed so that the address of the other variable can be assigned to the pointer 1.  The pointer acts as the function's return value function declaration: int *test () {} pointer can be used as the return value of the function, but cannot return the address of the local variable: to ensure that the returned pointer to the variable at the end of the function, there is still room for the solution: in the function of the heap area to request space, Return the address of the requested space but keep in mind that after the use is complete, the correct demo:
1 int*Test () {2     intarr =calloc(3,sizeof(int));3*arr =Ten;4* (arr+1) =Ten;5* (arr+2) = -;6     returnarr;7     //this is equivalent to applying for 3 int space in the heap, and then storing it as an array of {Ten, four, three};8     //returns an array of Arr at the first address of the heap area9     //after the function has ended, other callers will still be able to get Arr's address before encountering freeTen}

Note: 1. The return value of the function can be the value of the local variable, but not the address of the local variable 2. If the return value is of type char, char * Function name Example:
1 Char*getweekday (intDay ) {2     Switch(day) {3          Case 1:4         return"Monday";5          Break;6         //at this time, the return is a string, the string is directly present in the constant area, so there is no need to request a separate heap space7     }8}
There is no need to apply for additional heap space interpretation: the application in the constant area of space, is not recycled, until the program is received after the collection of character pointers stored in the constant area of the string data can not be changed, see ~user/desktop/teacher that came/day12 note/01-pointer and string 5 2. Pointer to function 1. When the program runs, it is loaded into memory 2. There is code in the program--and the code consists of a function 3. So the function must be somewhere in memory-code snippet 4. Since it is stored in memory, it must be stored in 1 of the corresponding memory space-then there will be a corresponding address 5. Then you can use a pointer to store the address of the function, that is, point to this function 6. Then use the pointer to call this function 7. Why? There are two ways to call the function 8. The declaration of a pointer to a function: 1. Not all function functions can point to the return value type and parameter seconds of the function being pointed to. Must be the same as the number of seconds of the pointer 2. The declaration syntax returns a value type (* pointer name) ([corresponding function parameter list]) example: void (*pfunction) (), and a function enumeration 1 that points to a name that does not have a return value without a parameter.     Introduction: Sometimes some variables can only take a certain number of limit values, when the ordinary variable can not meet the requirements C language also does not provide a variable to qualify the value type. We need to define ourselves a type with a qualified value--enumeration 2. Function: Support to create a new data type, the value of this type of variable is limited to 3. Syntax format
    1. Initialization
Enum new type name {qualified value 1, limit value 2, ...}; Cases:
1 enum direction{2    East, South, West,North3
    1. declaring variables
Enum Direction dir; In this way, the variable dir can only take one of the four values of E, S, W, N.
    1. Variable initialization
dir = East; 4. Precautions
    1. Scope: Each enumeration can only work within the body of the corresponding function, if you want to define it as a global
5. Some specifications
    1. Name naming conventions for enumeration types: uppercase letters, first letter of each word
    2. Naming conventions for enumeration values:
typedef aliases Give things aliases
    1. Use case: To thin the structure of the body name
      1. Declare the struct type first, and then use typedef to alias the struct type
      2. When declaring a struct strong, use a typedef to take an alias for the struct type
      3. For an anonymous struct (while declaring an anonymous struct) uses a typedef to go to a short alias for the struct type-the most common
    2. To thin the enumeration type
      1. Declare first, then alias
      2. Declare both aliases
      3. Anonymous Alias

The difference between c-const and static, pointers as function return values, pointers to functions, enumeration

Related Article

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.