Function
Creating your own functions must handle these 3 aspects yourself-Define, provide prototypes, and invoke.
A library function is a function that has already been defined and compiled, and it can provide its prototype using a standard header file, so you only need to call this function correctly.
C + + has certain limitations on the type of return value: it cannot be an array, but it can be any other type-integers, floating-point numbers, pointers, or even structures and objects! (You cannot return an array directly, but you can return an array as a struct or an object component.) )
The function prototype does not require a variable name, and a list of types is sufficient. Functional prototypes in C + + are essential.
Functions and Arrays
C + + interprets the array name as the address of its first element:
Cookies = = & Cookies[0]
cookies[i] = = * (cookies + i)
There are some exceptions to this rule:
1, the array declaration uses the array name to mark the storage location
2, the array using sizeof to get the length of the whole set
3, when the address character & user array name, will return the entire array address,
function prototypes for arrays
int Sum_arr (int arr[], int n)
int Sum_arr (int *arr, int n)
When passing a regular variable, the function uses a copy of the variable, but when the array is passed, the function uses the original array.
To prevent a function from accidentally modifying the contents of an array, you can use the keyword const when declaring a parameter
void show Array (const double ar[], int n);
Pointers and const
1, let the pointer point to a constant object, you can prevent the pointer to modify the point value.
int age =;
const int * pt = &age;
The PT declaration does not mean that the value it points to is actually a constant, but that it means that the value is constant for PT. For example, PT points to age, and age is not a const. You can modify the value of age directly through the age variable, but you cannot use the PT pointer to modify it.
Const can only prevent changes to the value pointed to by the *pt, but not the value of the PT. That is to say, a new address can be assigned to Pt.
You can assign a regular variable to a pointer to a const, so there are two possibilities: assigning the address of a const variable to a pointer to const, assigning a const address to a regular pointer.
const float G_earth = 9.80;
const FLOAT *PE = &g_earth; Valid
const float G_moon = 1.63;
float *pm = &g_moon; Invalid
2. Declare the pointer itself as a constant, which prevents you from changing the position that the pointer points to.
int sloth = 3;
const INT * PS = &sloth; A pointer to const int
int * Const FINGER = &sloth;//a const pointer to int
The keyword const position is different so that finger can only point to sloth, but allows finger to modify the sloth value. The middle declaration does not allow the use of PS to modify sloth values, but allows PS to be pointed to another location. In short, both finger and *ps are const, while *finger and PS are not.
3, pointers and values are constant
Double trouble = 2.0E30;
Const DOUBLE * Const STICK = &trouble;
At this point, stick can only point to trouble and cannot modify the trouble value with stick.
Functions and C-style strings
A C-style string consists of a series of characters that end with a null character. Array-related knowledge also applies to C-style strings. For example, using a string as a parameter means that the address is passed, or you can use const to prevent the string from being modified.
Passes a string as a parameter to a function, and there are 3 ways in which the string is represented:
char array;
A string constant enclosed in quotation marks (also a string literal);
The char pointer to the address that is set to the string
Char ghost[15] = "galloping";
char * str = "galumphing";
Passing a string as a parameter actually passes the address of the first character of the string. means that the function prototype should be declared as a char * type.
An important difference between a C-style string and a regular char array is that the string has a built-in end character, which means that the string length is not to be passed to the function as a parameter.
Functions and String objects
Although C-style strings and string objects are used almost identically, string objects are more similar to structs than arrays. For example, you can assign a struct to another structure, or you can assign an object to another object. You can pass the structure as a complete entity to a function, or you can pass the object as a complete entity. If you need more than one string, you can declare a string object array instead of a two-dimensional char array.
function and struct body
Unlike an array name, which is the address of the first element of an array, the struct name is only the name of the structure, and the address operator must be used to get the address of the structure. &
Address of the delivery structure
When a function is invoked, the address of the struct (&st) rather than the structure itself (ST) is passed to it;
The parameter is declared as a pointer to St, that is, the St * type. If you do not allow modifying the structure, you can use the const modifier;
Because the formal parameter is a pointer rather than a struct, the indirect member operator (->) is applied instead of the member operator (.).
function pointers
Similar to data items, functions also have addresses. The address of a function is the starting address of the memory that stores its machine language code. You can write a function that takes the address of another function as a parameter, so that the first function will be able to find the second function and run it. Function name is the address of the function