C language program design base lecture pointer's generous

xiaoxiao2021-03-06  21

The pointer is a data type widely used in the C language. Using the pointer programming is one of the most important style of C language. The use of pointer variables can represent various data structures; it is convenient to use array and string; and can process memory addresses like assembly languages ​​to compile the internal and efficient programs. The pointer greatly enriches the function of the C language. The learning pointer is the most important ring in the C language, and whether it can correctly understand and use the pointer to whether we have a sign of the C language. At the same time, the pointer is also the most difficult part in the C language. In addition to understanding the basic concepts in the study, it must be multi-programmed and debugged. As long as you do this, the pointer is not difficult to master.

The basic concept of the pointer is in the computer, and all the data is stored in the memory. Generally, one byte in the memory is called a memory cell. The number of memory cells occupied by different data types is not equal. If the integer amount is 2 units, the character quantity accounts for 1 unit, etc., in the second chapter Detailed introduction. In order to properly access these memory cells, the number must be made for each memory unit. The memory unit can be accurately found according to the number of a memory cell. The number of the memory cell is also called the address. Since the required memory unit can be found according to the number or address of the memory cell, this address is usually referred to as a pointer. The content of the memory cell and the content of the memory cell is two different concepts. You can use a popular example to explain the relationship between them. When we go to the bank to deposit, the bank staff will find our deposit slip according to our account. After finding it, it is found to write deposits on the deposit order, withdraw the amount. Here, the account is the pointer of the memory, and the number of deposits is the content of the memory. For a memory unit, the address of the unit is a pointer, where the data stored is the content of the unit. In the C language, a variable is allowed to store the pointer, which is called a pointer variable. Therefore, the value of a pointer variable is the address of a memory cell or a pointer called a memory unit. In the figure, character variable C is provided, its content is "K" (ASCII code is a decimal number 75), and C occupies the 011A unit (address hexadecimal representation). It is provided with a pointer variable P, which is 011A. This condition we call P pointing to variable C, or P is pointed to the variable C. Strictly speaking, a pointer is an address and is a constant. And a pointer variable can be given different pointer values, which is changed. However, the pointer variable is often referred to as a pointer. In order to avoid confusion, we agree that: "pointer" means the address, the constant, "pointer variable" means a variable of the value of the address. The purpose of defining a pointer is to access memory cells through a pointer. Since the value of the pointer variable is an address, then this address can be the address of the variable, but also the address of other data structures. What is the meaning of the first address stored in a pointer variable or a function? Because array or functions are continuously stored. The array or function is found by accessing the argument variables, and the array or function is found. In this way, any array, where the function can be expressed in a pointer variable, as long as the pointer variable gives an array or the first address of the function. In doing so, the concept of the program will be very clear, the program itself is also refined, efficient. In C language, a data type or data structure often occupies a set of continuous memory cells. With the concept of "address", it does not describe a data type or data structure, and "pointer" is actually an address, but it is the first address of a data structure, it is a "pointing" a data structure. And thus concept is more clear, indicating more clear. This is an important reason for introducing the concept of "pointer". Type of pointer variable

Type description of pointer variables includes three contents:

(1) The pointer type description, that is, the defined variable is a pointer variable;

(2) Pointer variable name;

(3) The data type of the variable pointed to by the variable value (pointer). Its general form is: Type specifier * variable name;

Where * indicates that this is a pointer variable, the variable name is the pointer variable name defined, and the type indicator indicates the data type of the variable pointed to this pointer variable.

For example: int * p1; indicating that P1 is a pointer variable, its value is the address of a integer variable. Or say P1 points to a integer variable. As for the P1, which integer variable is pointed to, it should be determined by the address given to P1.

Rethlean:

Staic Int * P2; / * P2 is a pointer variable to static integer variable * / float * p3; / * P3 is a pointer variable of floating point variable * / char * p4; / * P4 is a pointer variable to character variables * / It should be noted that a pointer variable can only point to the same type of variable, such as P3 can only point to floating point variables, and point to a floating point variable, sometimes pointing to a character variable.

Assignment of pointer variables

The pointer variable is the same as normal variables, not only the description is used before use, but must give a specific value. Unfailed pointer variables cannot be used, otherwise the system will cause system confusion, or even crash. The assignment of the pointer variable can only give an address, and it will never give any other data, otherwise an error will cause an error. In the C language, the address of the variable is assigned by the compilation system, which is completely transparent to the user, and the user does not know the specific address of the variable. The address operator & to represent the address of the variable in the C language. Its general form is: & variable name; if & a address of the variable A, & b represents the address of the variable B. The variable itself must be described in advance. There is a pointer variable P pointing to the integer variable, and the address to which the integer variable A is to be given can have the following two ways:

(1) Method for initialization of pointer variables

INT A; int * p = & a;

(2) Method for assigning statements

INT A; int * p; p = & a;

It is not allowed to give a number of pointer variables, so the following assignment is wrong: int * p; p = 1000; before being assigned pointer variables, no "*" specifier, such as writing * p = & a is also wrong

Calculation of pointer variables

The pointer variables can be made, but the types of operations are limited. It can only perform assignment operations and partial arithmetic operations and relationships.

Pointer operator

(1) Take address operators &

Take the address operator & is an unique operator, its combination is from right to left, and its function is the address of the variable. In the Scanf function and the previous introduction of the pointer variable assignment, we have known & operators.

(2) Take the content operator *

Take the content operator * is an unique operator, its combination is from right to left, used to represent variables referred to in pointer variables. The variables followed by the * operator must be a pointer variable. It should be noted that the pointer operator * and pointer variable descriptions are not one thing. In the pointer variable description, "*" is a type specifier, indicating that the variables thereafter are the pointer type. The "*" appearing in the expression is an operator to indicate the variable referred to in the pointer variable.

Main () {Int a = 5, * p = & a; printf ("% d", * p);} ...

The pointer variable P acquiescence the address of the integer variable A. This statement represents the value of the output variable A.

2. Computing of pointer variables

(1) Assignment operation

The assignment calculation of pointer variables has several forms:

1 Pointer variable initialization assignment, which has been described earlier.

2 Give the address of a variable to the pointer variable to the same data type. E.g:

INT A, * Pa; PA = & a; / * Give the address of the integer variable A to the integer pointer variable PA * /

3 Assign a value of a pointer variable to another pointer variable to the same type variable. Such as:

INT A, * PA = & A, * PB; PB = Pa; / * Give the address of A to the pointer variable PB * / because PA, PB is pointed to the integer variable, so it can be assigned each other.

4 Give the first address of the array to the pointer variables of the array.

E.g:

INT A [5], * Pa; PA = A; (Number of group names indicate the first address of the array, so that the pointer variable PA of the array can be given.

It can also be written as:

PA = & a [0]; / * The address of the first element of arrays is also the first address of the entire array.

You can also give PA * /

Of course, the method of initialization assignment can be taken:

INT A [5], * PA = a;

5 Give the first address of the string to point to the pointer variable of the character type. For example: char * pc; pc = "c language"; or by initial assignment method is written as: char * pc = "c language"; herein should be noted to load the entire string into pointer variables, but The first address of the character array stored in the string is loaded into the pointer variable. Also described later.

6 Gring the entry address of the function to the pointer variable that points the function. For example: int (* pf) (); pf = f; / * f is a function name * /

(2) Adding and subtracting the arithmetic operation

For pointer variables pointing to array, you can add or subtract an integer n. Setting PA is a pointer variable pointing to array A, then PA N, PA-N, PA , Pa, PA -, - PA operations are legal. The meaning of the pointer variable or minus an integer N is to move the current position (point to a array element) pointing forward or backward. It should be noted that the array pointer variable moves forward or rearward and adds 1 or minus 1 in concept. Because arrays can have different types, the length of the byte lengths of various types of array elements are different. If the pointer variable plus 1, the backward moves 1 position indicates the first address of the pointer variable points to the next data element. Instead of adding 1 on the basis of the original address.

E.g:

INT A [5], * Pa; PA = a; / * Pa pointing to array a, also pointing to A [0] * / PA = PA 2; / * Pa pointing a [2], that is, the value of Pa [is & pa [ 2] * / The addition and decrease of the pointer variable can only be carried out on the array pointer variable, which is meaningless to the pointer variables to other types of variables. (3) The operation between the two pointer variables can only be operated between the two pointer variables to the same array, otherwise the operation is meaningless.

1 two pointer variables

The difference between the two pointer variables is subjected to the number of elements of the two pointers index index elements. In fact, the difference between the two pointer values ​​(addresses) is then divided by the length of the array element (byte). For example, PF1 and PF2 are two pointer variables to the same floating point array. The value of PF1 is 2010 h, and the value of PF2 is 2000 h, and the floating point array accounts for 4 bytes, so the result of PF1-PF2 is ( 2000H-2010H) / 4 = 4, indicating a difference between PF1 and PF2. Two pointer variables cannot be added. For example, what does PF1 PF2 mean? There is no practical meaning.

2 Two pointer variables

The relationship between the two pointer variables to the same array can represent the relationship between their index group elements. E.g:

PF1 == PF2 indicates that PF1 and PF2 points to the same array element PF1> PF2 indicate that PF1 is in high address position PF1

Assign a value to the pointer variable PA, PA points to the variable A.

Assign a value to the pointer variable PB, PB points to the variable B.

The significance of the Bank is the sum of A B, (* PA is A, * PB is b).

The Bank is the accumulation of A * B.

Output results.

Output results.

......

The pointer variable can also be compared with 0. Set P as a pointer variable, p == 0 indicates that the P is an empty pointer, which does not point to any variable; p! = 0 means P is not empty pointer. The empty pointer is obtained by assigning 0 values ​​to the pointer variable. For example: #define null 0 int * p = null; 0 values ​​for pointer variables and non-assignment are different. When the pointer variable is not assigned, it can be any value, it is not possible. Otherwise it will cause an unexpected error. After the pointer variable is 0, it can be used, but it does not point to the specific variable.

Main () {Int A, B, C, * PMAX, * Pmin; Printf ("Input Three Numbers: / N"); Scanf ("% D% D% D", & A, & B, & C); if (A > b) {pMax = & a; pmin = & b;} else {pmax = & b; pmin = & a;} if (c> * pmax) PMAX = & C; IF (c <* pmin) Pmin = & C; printf ("max =% D / nmin =% D / N ", * pmax, * pmin);} ...

PMAX, Pmin is an integer pointer variable.

Enter a prompt.

Enter three numbers.

If the first number is greater than the second number. . .

Pointer variable assignment

Pointer variable assignment

Pointer variable assignment

Pointer variable assignment

Judgment and assign

Judgment and assign

Description and use of array pointer variables

The pointer variable pointing to the array is called the array pointer variable. Before discussing the description and use of array pointers, we must clear a few relationships.

An array is composed of a continuous memory unit. The array name is the first address of this continuous memory unit. An array is also composed of each array element (subscript variable). Each array element has several consecutive memory cells in its type. The first address of an array element refers to the first address of several memory cells it occupies. A pointer variable can be directed to an array or pointing to an array element that imparts the address of the array name or the first element. To make the pointer variable point to the first address of the I element to give it or give it an array name.

There is a real number A, pointing to a pointer variable of A. From Figure 6.3 we can see that there is a relationship:

PA, A, & A [0] points to the same unit, and they are the first address of array A and the first address of element 0 element a [0]. PA 1, A 1, & A [1] points to 1 element a [1]. Category A I, A I, & A [I]

Points I number I element a [i]. It should be noted that PA is variable, and a, & a [i] is constant. Pay attention to the programming.

Main () {Int A [5], I; for (i = 0; i <5; i ) {a [i] = i; printf ("a [% d] =% D / N", I, A [i]);} Printf ("/ n");} main function

Define a integer array and a integer variable

loop statement

Assign arguments to array

Print each array value

......

Output wrap

......

The general form of array pointer variable description is:

Type Description * Pointer Variable Name

The type of type indicator indicates the type of the index group. From a general form, it can be seen that the pointer variables of the array and the description of the pointer variables to ordinary variables are the same. After introducing the pointer variable, you can use two ways to access the array element.

The first method is the subscript method, that is, the array element is accessed in the form of a [i]. This method is used when the array occurs in Chapter 4.

The second method is a pointer method, i.e., in the form of * (PA I), using indirect access to the array element.

Main () {Int A [5], I, * Pa; PA = a; for (i = 0; I <5; I ) {* PA = I; PA ;} PA = a; for (i = 0; I <5; i ) {Printf ("a [% D] =% D / N", I, * PA); PA ;}}

Main function

Define integer arrays and pointers

Point pointer PA to array a

cycle

Assign the value of the variable I to the array unit of the a [] pointed to by the pointer PA.

Point pointer PA points to the next unit of a []

......

The pointer PA re-acquires the first address of the array A

cycle

Output all elements in array a in an array mode

Point pointer PA points to the next unit of a []

...... ...

Next, an example is also given, the example is the same as the above, but the implementation is different.

Main () {Int a [5], I, * PA = a; for (i = 0; I <5;) {* PA = I; Printf ("a [% d] =% d / n", i , * PA );}}

Main function

Define integer arrays and pointers, and make pole point to array a

cycle

Assign the value of the variable I to the array unit of the a [] pointed to by the pointer PA.

Use the pointer to output all the elements in the array A, and the pointer PA points to the next unit of a []

...... ...... A number of group name and array pointer variable function parameters

In Chapter 5, I have introduced the issue of the unusual number of group named functions. This problem is easier to understand after learning the pointer variable. The number of group names is the first address of the array. The actual parameter gates are actually the address of the transfer array, and the shape is also directed to the same array. This is like the same items, which are both different from each other. Similarly, the value of the pointer variable is also the address, the value of the array pointer variable is the first address of the array, and of course, as a function of the function.

FLOAT AVER (Float * Pa); main () {float sco [5], av, * sp; int 1; sp = sco; printf ("/ ninput 5 score: / n"); for (i = 0; i <5; i ) scanf ("% f", & sco [i]); av = AVER (SP); Printf ("Average Score IS% 5.2F", AV);} Float Aver (FLOAT * PA) {INT i ; Float av, s = 0; for (i = 0; i <5; i ) s = S * PA ; AV = S / 5; Return AV;}

转载请注明原文地址:https://www.9cbs.com/read-44086.html

New Post(0)