C ++: function-C ++ programming module

Source: Internet
Author: User

1. function prototype: The function prototype describes the function-to-compiler interface, that is, it informs the compiler of the type of the function return value and the number of parameter types.

2. pointer and const:

There are two kinds of const usage: one is to point the pointer to a constant object (the pointer refers to const), and the other is to declare the pointer itself as a constant (the pointer is const ).

Int age = 39;

Const int * PT = & age;

Int const * PT and const int * PT are identical.

This statement indicates that PT points to a const int (39 in this example), so PT cannot be used to modify this value, that is, * the PT value is const, cannot be modified (the age cannot be modified through * PT

Value), but you can modify the value of age through age.

In C ++, do not assign a const address to a non-const pointer. For example:

Const float fg = 1.54;

Float * PM = & FG; // This assignment is invalid.

The following assignment is correct:

Cosnt float fg = 1.54;

Const * PM = & FG;

Note: If the data type is not a pointer, you can assign the addresses of const data or non-const data to the pointer to const, however, only the addresses of non-cosnt data can be assigned to non-const pointers.

Some other advantages of pointers:

See the following statement:

Int num = 39;

Cosnt int * PT = & num;

In the second statement above, cosnt can only prevent modification of the value pointed to by PT, but cannot prevent modification of the PT value, that is, a new address can be assigned to PT:

Int age = 34;

PT = & age;

However, you still cannot use pt to modify the value it points.

The second method is const, so that the pointer value cannot be modified:

Int sum = 440;

Const int * PT = & sum;

Int * const PM = & sum;

In the third declaration above, the PM can only point to sum, but the run uses pm to modify the sum value. The second declaration does not allow the use of PT to modify the sum value, but the run points pt to another

Address. In short, both PM and * PT are const (constants), while * pm and PT are not.

3. array and structure: the array name is an address, but the structure name is not. To obtain the structure address, you must use an access Letter &.

4. function pointer:

Function pointer Declaration: Usually you need to declare a pointer to a specific type of function. You can first write the prototype of the function, and then replace the function name with (* FP). In this way, FP is the pointer to this type of function.

Example:

Double PAM (INT );

Double (* FP) (INT );

(* FP) must be enclosed in parentheses. Because the priority of parentheses is higher than *, * FP (INT) means that FP () is a function that returns pointers, and (* FP) (INT) refers to a function.

Needle. After the declaration, you can assign the address to the pointer: fp = PAM;

Pointer function call:

When using a function pointer, you only need to think of it as the function name, that is, (* FP) as the function name:

Double PAM (INT );

Double (* FP) (INT );

Fp = PAM;

Double X = PAM (4 );

Double Y = (* FP) (5 );

Pointer function: a function that returns a pointer.

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.