C-function pointers, pointer functions, structural bodies of structures

Source: Internet
Author: User

Pointer function: The return value is the definition of the function pointer function of the pointer: general form: type specifier * Function name (formal parameter list) {function Body} where the function name is preceded by a ' * ' indicating that it is a ' pointer-type ' function, that is, the return value is a pointer. The type descriptor represents the data type function pointer that the returned pointer points to:
    1. A function always occupies a contiguous memory area in memory, and the function name is the first address of the memory area that the function occupies--the first address of the function (the entry address) is given a pointer variable, which points the pointer variable to the function. The function can then be found and called by a pointer variable--this pointer to the function variable is called the function pointer variable.
Type specifier (* pointer variable name) (parameter of function); type specifier: is the parameter of the function that is the return value type of the function: is the parameter of the function that points to the above two if none (void and no argument), the pointer declaration can also change when the function name is equivalent to the function in the header (*P) Declaration
1 int sum (intint  b); 2 int (*p) (intint b);
 Initialization: p1 = sum; The memory is the first address function pointer variable Note:
    1. function pointer variables cannot perform arithmetic operations, which are different from array pointer variables. Array pointer variable plus minus an integer allows the pointer to move to the next or previous array element, while the move of the function pointer is meaningless (the address movement in the function body is basically meaningless)
    2. The parentheses of the "(* pointer variable name)" In a function call are not limited, where the * is not a constant, as in the case of a pointer variable declaration, it is just a notation indicating that the variable is a pointer variable
Start of construction Type:
    1. What is a constructed data type: a constructed data type is defined by a constructed method based on one or more defined data types-that is, a value of a constructed type can be decomposed into several "members" or "elements". Each member is a basic data type or another constructed type
2. Typical construction types: array structure struct common body (Union) Union 3. Struct: An array of defects: a data structure body that can store only the same type: the equivalent of a record in another high-level language is a constructed type, consisting of several "members" Each member can make a basic data type or another construct type 4. For the purpose of the structure:
    1. You can ignore data types and organize the same data together to make them easier to use.
    2. When calling a function, if the passing parameters are more, it is relatively simple to pass a struct, and many of the system's own functions must use the structure
    3. Role: You can create a new data type that specifies which urine volume is the combination of the new data type's variables.
5. Statement of the structural body
1 structstruct Name {2     //member List3 };4 5 //define a student's structure6 structstu{7     intnum;8     Charname[ -];9     Charsex;Ten     floatscore; One};
Notice at the time of declaration: there is a semicolon behind the structure curly brace, and you cannot omit 6. Attention
    1. Once the struct definition is complete, the computer does not allocate memory space to the struct, but allocates the memory space after the struct variable is defined
    2. Structs are a new type of data that is defined, so before you can use struct variables, you need to define the type before declaring the variable
    3. Naming conventions: Capitalize the first letter of each word
7. Defining a variable of a struct type
    1. Define struct body variables after definition
struct Stu stu1;  This sentence means that the definition of a STU1 struct type variable//STU1 is because it is the Stu type, so STU1 can store students ' student number, name, age, scores; It is also possible to define more than one struct variable struct stu stu2, Stu3, Stu4;
    1. Define struct-body variables while defining structs
1 struct car{2     int num; 3     Char name; 4 }car1, car2, CAR3;
    1. Define struct variables with anonymous struct-body
1 struct {2     int num; 3     Char color; 4 }car1, car2, CAR3;

Try not to use this method
    1. Problems with default values:
      1. Only declares uninitialized: all garbage values
      2. Partial/Full initialization: Uninitialized values are automatically assigned 0
    2. Scope issues:
      1. In general, as with normal variables, the code blocks that are defined are invalidated.
      2. And a struct is generally more than a reference, so the general situation is the global
8. Structure array
    1. Declaration: struct element type array name [array length]
struct Student students[5]; Indicates that we have declared an array of length 5, the name of the array: students, the length of the array: 5, the type of the array element: struct Student, so that this array can store 5 elements of a struct Student type
    1. Initialization
      1. Declare later elements are initialized individually
      2. Declaring simultaneous full initialization/partial initialization
9. Structure pointer
    1. Format: struct structure type name * Pointer name →struct Student *pstu
A struct Student pointer variable is declared, and the type of the pointer variable is struct Student, which can only point to a variable of struct Student type
    1. Initialization steps:
      1. Remove the address of the struct variable
        1. &
      2. Assigning an address value to a pointer variable
        1. struct Student xiaoming = {"Xiaoming", 86};
        2. struct Student *pstu = &xiaoMing;
    2. struct pointer accesses struct variable: see LH20160309/STRUCT array structure
10. Structure nesting
    1. Simple, slightly
11. Structure and function
    1. A struct is a value passed as a parameter.
    2. If you want the inside of a function to be able to argument the value of a struct variable, the struct pointer as a parameter
    3. As the return value:

  

1  struct Student getastudent () {2      struct Bayi }; 3      return S1; 4  5//  cannot return the address of the S1 because, as a local variable, the function calls its address to release 6                                 7 // If you want to keep its address constant, analogy with the string pointer, calloc in the heap area to request space, remember free.

C-function pointer, pointer function, struct structure body

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.