C + + Basic Review 2 (functions, pointers, and references)

Source: Internet
Author: User
Tags function prototype

Then review the functions, pointers, and applications.

Function

1, multidimensional arrays as formal parameters, the size of the first dimension can be omitted (or not omitted), but the size of the other dimensions must be specified. such as two-dimensional array parameters, int array[3][] is incorrect, int arry[][10] is correct. Because the argument comes from the beginning address of the array, which is stored in memory by row, but not the branch and column, if the number of columns is not specified in the parameter, the system cannot determine how many rows should be.

2. In C + +, you can specify a default value for the parameter, and the default value is automatically used when a function call does not specify an argument corresponding to the formal parameter. The default parameters for C + + functions can be constants, or global variables or global constants, or even a function call. The default parameter can only occur once, and if given in the function prototype, it cannot be given again in the function definition. If the function is defined before the function call and the function declaration is not in the program, the default value should be given in the function definition; otherwise, if the default value of the function is not specified in the function prototype, and the default value is given in the definition for the parameter, the default value is not used when the call is made. If there are multiple parameters in a function, the default parameters should be defined from right to left.

3. The definition of an inline function must appear before the inline function is first called, because the compiler replaces the function call statement with the function body of the inline function at compile time.

4. A function cannot be both an overloaded function and a function with default parameters. such as: int Mymax (int x, inty); int Mymax (int x, int y, int z = 100), when the function is called without a parameter, such as call "Mymax", the compilation system can not determine whether the use of overloaded functions or the use of default parameters of the function, there is two semantics, the system cannot execute.

5. Using the global Operator "::", you can access global variables in the local scope. For example, there is a global variable x, and there is a local variable x, where you want to use the global variable x in the scope of the local variable, then use ":: X".

6. Static variables are pre-allocated at the time of program compilation and are determined by the storage unit before the program executes. When defining a static local variable, if the initial value is set at the same time, the initial value is defined before the program starts executing, and the initial values are no longer reset each time the function is called, but at the end of the last function call.

7, if the static variable is defined, the initial value is not specified, then the system automatically assigns a static variable to the initial value of the binary information is all 0. (But in order for the program to be easy to transplant, read and modify, you should explicitly give the initial value of the static local variables in the program)

8. Static global variables are accessible to functions in the same source program file, but unlike general global variables, they cannot be accessed by functions in any other source program file. (Static functions also have the same access restrictions)

9, register variable (register): only int/char/pointer type local variables and formal parameters can be register storage class. The storage class of a local variable is specified as a register, which is a reminder compiler, which is used very frequently in the program, when allocating memory for the variable, it is possible to allocate a register for it whenever possible, because the access register is faster than accessing the storage unit. The storage class for a formal parameter is specified as a register, possibly because the driver of these devices requires that the information be passed to the system with a register as a parameter in order to access some special drivers.

10, you can use the "# undef identifier" command to terminate the scope of the macro definition.

11, macros with parameters, macro expansion is the name of the macro in the program in parentheses after the argument from left to right in place of the parameter in the macro problem.

Example: #define PI 3.1415

#define S (r) = PI * R * r

If you call S (10.0 + 10.0), you cannot get the result we expect. The actual result is: 3.1415 * 10.0 + 10.0 * 10.0 + 10.0. If you use an inline function, you can solve this problem.

Pointers and references

1. The elements in the array can be found with pointers or subscripts. The subscript method is more intuitive, but it takes more time to find array elements. In particular, when iterating over the element values in an array, it is not necessary to repeat the address every time by using the pointer rule.

2. Pointer to constant: A pointer variable pointing to a constant. Example: const char *name = "Fang"; Then you cannot use the following similar statement name[3] = ' a '; But since name is a normal pointer variable pointing to a constant, not a regular pointer, you can change the point of name, and the following statement is the allowed name = "Pei".

3. Constant pointer: Declares the pointer itself as a constant, rather than the life of the object it points to. Creating a constant pointer is creating a fixed pointer that cannot be moved, but the data it refers to can be changed. Must be initialized when defining a pointer constant.  Example: char * const NAME = "Fang"; Allow name[3] = ' n ', name = "Pei" is not allowed.

4. Constant pointers to constants: The pointer itself cannot be changed, and the value pointed to cannot be changed. Example: const char * Const NAME = "Fang".

5. Reference: A reference is an alias to a variable or constant identifier. int Val; int &rval =val; Then Val and Rval refer to the same variable, and they are used exactly the same way.

6. When the compiler sees "&", it does not allocate memory space for the identifier that follows it, but simply assigns it the memory space of the identifier it refers to.

7. When declaring a reference type variable, it must be initialized at the same time, that is, the object it refers to must be stated when the reference is declared. The object being referenced must already have a corresponding memory space. (a function parameter is a reference type, "not initialized" because the argument is assigned to the parameter when the function is called)

8. A reference to a void type cannot be declared (because void itself means no data type, and its reference is meaningless); You cannot reference a group name (because the array name represents the starting address, it is not a variable by itself); You cannot define a pointer to a reference type (because the reference itself is just a symbol, It does not have any memory space).

9. When you qualify a reference with a const, it means that the value of the referenced space cannot be changed by reference. In addition, when referencing a constant variable, you must define the reference as Const.

10, the largest use of reference is as a function of the parameter or return value type, thereby extending the function to pass data. Note that the argument corresponding to the reference parameter must have a corresponding memory space, that is, the argument must have a valid memory space in order to be able to reference the space. The reference function parameter is usually in the following two occasions: 1) function needs to return multiple values of the occasion; 2) The parameter of the function is structure or class, because it will occupy more memory space at this time, if passing by value will need to allocate more stack space to hold the value of formal parameter, a large amount of data copy operation is needed, which will consume more space and time. (In fact, these two occasions use pointers can achieve the same effect)

C + + Basic Review 2 (functions, pointers, and references)

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.