C Programming Language (IX)

The ninth chapter pointer

, Each memory cell has a unique address C program variables are stored in the computer memory of a specific storage unit

By address operator & possible to obtain the address of a variable

 

// L9-1

#include <stdio.h>
int main()
{
    int  a = 0, b = 1;
    char c = 'A';
    printf("a is %d, &a is %p\n", a, &a);
    printf("b is %d, &b is %p\n", b, &b);
    printf("c is %c, &c is %p\n", c, &c); return 0; }

 

// Run Results 
A IS 0, IS A & 0060FF0C 
B IS. 1, B IS & 0060FF08 
C IS A, C & IS 0060FF07

 

 

% P specifier indicating the address value of the output variables a, b, c of

Address value is a hexadecimal unsigned integer, which is the word length is generally the same as the word length of the host

 

  • Address variables: the first address of the storage space occupied by the variables in memory
  • Value of the variable: the variable data stored in the storage space
  • Variable names: an abstract of data storage space program

 

Pointer: a special type of variable to store the address of the variable

 

Type a keyword * pointer variable name;

 

 

Type a keyword that represents the data type of the variable pointed to the pointer variable, i.e., the base type of the pointer variable

 

Defined pointer variable is only declared the data type of the pointer variable name and can point to, and did not specify whether the pointer variable points to where

 

// L9-2

#include <stdio.h> 
int main () 
{ 
    int A = 0, B =. 1 ; 
    char C = 'A' ; 
    int * pa, Pb *; / * define a pointer variable * pa and Pb / 
    char * PC; / * definition of a pointer variable pc * /  pa = & A; / * initialize the pointer variable pa to point A * /  pb = & B; / * initialize the pointer variable pb to point B * / pc = & C; / * initialize the pointer variable pc so which points to C * / the printf ( "A IS% D, & A IS% P, PA IS% P, & PA IS% P \ n-", A, & A, PA, & PA); the printf ( "B IS% D, & B IS% P, Pb IS% P, & Pb IS% P \ n-", B, & B, Pb, & Pb); the printf (" C IS% C, & C IS% P, PC IS% P, & PC IS% P \ n-", C, & C, PC, & PC); return 0 ;}

 

//运行结果
a is 0, &a is 0060FF0C, pa is 0060FF0C, &pa is 0060FF00
b is 1, &b is 0060FF08, pb is 0060FF08, &pb is 0060FEFC
c is A, &c is 0060FF07, pc is 0060FF07, &pc is 0060FEF8

 

 

The value of the pointer variable uninitialized pointer variable means is a random value, can not predict where it will point

To avoid forget pointer to initialize the system potentially dangerous, in the definition of a pointer variable at the same time it is customary initialized to NULL

The int * pa = NULL;

In the definition of indicator variables can be initialized while the pointer variables

如int *pa = &a; 

 

Indirection operator:

  • Direct Addressing: by address variable or variables to access the contents of a variable direct access mode
  • Indirect Addressing: The pointer variable access method to indirectly access the underlying variable

 

Value indirection operator access to the pointer variable * variable pointed

 

// L9-3

#include <stdio.h> 
int main () 
{ 
    int A = 0, B =. 1 ; 
    char C = 'A' ; 
    int * & PA = A, * = Pb & B; / * pointer variable is defined pa, pb and pc while its initialization * / 
    char * PC = & C; // * a pointer type specifier for defining a pointer variable 
    printf ( "a is% d, & a is% p, pa is% p, * pa is% d \ n-", a, a &, PA, * PA);  // * indirect reference operator as, for reading and displaying the value of the pointer stored in the variable memory address corresponding to the variable  printf (" b is% d, & B IS% P, Pb IS% P, * Pb IS% D \ n-", B, & B, Pb, * Pb);  the printf (" C IS% C, & C IS% P, PC IS% P, * PC IS C% \ n-", C, & C, PC, * PC); return 0 ;}

 

//运行结果
a is 0, &a is 0060FF00, pa is 0060FF00, *pa is 0
b is 1, &b is 0060FEFC, pb is 0060FEFC, *pb is 1
c is A, &c is 0060FEFB, pc is 0060FEFB, *pc is A

 

 

* Pa output value and outputs a value equivalent, thus modifying the value of * pa it is equivalent to the value of a modified

Pointer dereferencing : reference value of the pointer variable in the variable points

 

Only after the real pointer pointing to a piece of meaningful memory to access its contents

 

Use pointers need to abide by:

  1. Always clear where each pointer points to
  2. Always clear what the contents of each pointer to the object is
  3. Never use an uninitialized pointer variable

 

Call by value

// L9-4

#include <stdio.h> 
void Fun (int PAR); 
int main () 
{ 
    int = Arg. 1 ; 
    the printf ( "Arg D =% \ n-" , Arg);  Fun (Arg); / * argument values passed copy to the function * /  the printf ( "Arg =% D \ n-" , Arg); return 0 ;} void Fun (int PAR) {the printf ( "PAR =% D \ n-" , PAR); PAR = 2; / * changing the parameter value * / }

 

// operating results 
Arg = 1 
PAR = 1 
Arg = 1

 

 

Changing the parameter value of the function argument values ​​did not affect the change in

This is because it is only a reference value of real-valued function call statement passed to the function parameter of the copy

Thus, it can not change the value of the argument in the call statement in the called function values ​​by the method invocation

 

Simulated call by reference

// L9-5

#include <stdio.h> 
void Fun (int * PAR); 
int main () 
{ 
    int = arg. 1 ; 
    the printf ( "% D = arg \ n-" , arg);  Fun (& arg); / * variable transmission of arg address value to the function * /  the printf ( "Arg =% D \ n-" , Arg); return 0 ;} void Fun (int * PAR) {the printf ( "PAR =% D \ n-", * PAR); / * output parameter value of the variable points * / * par = 2; / * changes the value of a variable parameter points * / }

 

// Run Results 
Arg. 1 = 
PAR. 1 = 
Arg = 2

 

 

The declaration of the function parameter is a pointer type, as a function pointer variable parameter

The received parameter data is only an address value

Fun Function () * indirection operator using the changed parameter values ​​of the argument point

 

SIMULATION call by reference is a common function that returns the modified data values

 

// L9-6

#include <stdio.h> 
void Swap (X int *, int * Y); 
int main () 
{ 
    int   A, B; 
    the printf ( "Please Enter A, B:" );  Scanf ( "% D,% D" , & a, & B);  the printf ( "the before the swap: a =% D, B =% D \ n-", a, B); / * before printing the exchange a, B * / Swap (& a, & B); / * according to the function call address swap () * / the printf ( "the After the swap:% A = D, B = D% \ n-", a, b); / * verify a, b whether or not swapping * / return 0 ;} / * function: swap values of an integer * / void Swap (X int *, int * Y) {int TEMP; * X = TEMP; step / * executes 7-5 (b) in ① * / * x = * y; / * 7-5 executes step ② (b) in * / * y = temp; / * executes steps 7-5 (B) ③ * / }

 

//运行结果
Please enter a,b:15,8
Before swap: a = 15, b = 8
After swap: a = 8, b = 15

 

 

Pointer variables as a function of the parameters

// L9-7

#include <stdio.h> 
#define N 30 
void FindMax (Score int [], Long NUM [], n-int, int * pMaxScore, Long * pMaxNum); 
int main () 
{ 
    int   Score [N], MaxScore; 
    int  n-, I;  Long  NUM [N], maxNum;  the printf ( "How MANY students.?" ); Scanf ( "% D", & n-); / * input from the keyboard the number of students n-* / the printf ( "the input student apos ID and Score : \ n-" ); for (I = 0; I <n-; I ++ ) {Scanf ("% LD% d ", & [I] NUM, & Score [I]); / * before the letter d letter L * / } FindMax (score, num, n, & maxScore, & maxNum); / * invoke the function at the address * / the printf ( "% D = MaxScore, maxNum LD =% \ n-" , MaxScore, maxNum); return 0;} / * Function: calculating the highest points and the corresponding number of students in the school * / void FindMax (Score int [], Long NUM [], n-int, int * pMaxScore, Long * pMaxNum) {int I; * = pMaxScore score [0]; / * assume score [0] is the current highest score * / * pMaxNum = num [0 ]; / * record score [0] student number num [0] * / for ( i = 1; i < n; i ++) / * for all score [i] is compared * / {IF (score [i]> * pMaxScore) / * If the score [i] higher than the current highest score * / {* pMaxScore = score [i]; / * modify the current highest score * / * pMaxNum = num [i ] with score [i]; / * record the current highest score students' learning number NUM [I] * / }}}

 

//运行结果
How many students?5
Input student's ID and score:
120310122 84
120310123 83
120310124 88
120310125 87
120310126 61
maxScore = 88, maxNum = 120310124

 

 

FindMax function () of the first three parameter whose value must be determined before the function call, the function so called parameter entry

And the value of the pointer variable to be determined after completion of the function call, so called pointer parameter argument function outlet

 

Function pointers: a pointer to a function

Pointers to functions stored in the variable is a function entry address in memory

The function name is the function of the source code in memory starting address

The compiler will not function name with () is interpreted as the entry address of the function

 

// L9-8

#include <stdio.h> 
#define N 40 
int ReadScore (Score int []); / * function prototype performance input * / 
void PrintScore (Score int [], int n-); / * output score function prototype * / 
void AscendingSort (int a [], int n ); / * Sort ascending function prototypes * / 
void DescendingSort (int a [], int n ); / * Sort descending function prototypes * / 
void Swap (int * X, int * Y); / * number of two switching function prototypes * / 
int main () 
{ 
    int Score [N], n-; 
    int Order; / * value of 1 indicates the ascending order, a descending sort is represented by 2 * / 
    n-ReadScore = (Score); / * enter the results, return the number of students * /  printf ( "Total students are% d \ the n-" , the n-);  printf ( "the enter 1 to the Sort in Ascending the Order, \ the n-" ); printf ( "the enter 2 to the Sort in Descending the Order : "); Scanf ( "% D", & Order); the printf ( "in the Data items Original Order \ n-" ); PrintScore (Score, n-); score / * output before ordering * / IF (Order ==. 1 ) { AscendingSort (score, n); / * in ascending order * / the printf ( "the Data items in ascending Order \ n-" );} the else {DescendingSort (Score, n-); / * Sort descending * / the printf ( "the Data items in Order Descending \ n-" );} PrintScore (score, n-); / * score outputting sorted * / return 0 ;} / * function: input students in a course of performance, when the negative input, the input ends, returns the number of students * / int ReadScore (int Score []) {int I = -1 ; do {I ++ ; the printf ( "the Input Score:" ); Scanf ( "% D", & Score [I]);} the while ( score [I]> = 0 ); return I;} / * function: output student performance * / void PrintScore (int score [ ],int n) { int i; for (i=0; i<n; i++) {The printf ( "% 4D" , Score [I]);} the printf ( "\ n-" );} / * Function: ascending order to achieve selection method of the array a * / void AscendingSort (int a [ ], int n ) / * in ascending order function definition * / {int I, J, K; for (I = 0; I <n--. 1; I ++ ) {K = I; for (J = I +. 1; J <n-; J ++ ) {IF (a [J] <a [K]) K = J;} IF (K = I!) Swap (& a [K], & a [I]);}} / * function: select the method implemented array a sorted in descending order * / void DescendingSort (int a [ ], int n) / * descending sort function definition * / {int I, J, K; for (I = 0; I <n--. 1; I ++ ) {K = I ; for (J = I +. 1; J <n-; J ++ ) {IF (A [J]> A [K]) K = J;} IF (! K = I) Swap (& A [K], & A [I ]);}} / * function: two interchangeable integer * / void swap (X int *, int * Y) {int TEMP; * TEMP = X; * = X * Y; Y * = TEMP;}

 

//运行结果
Input score:90
Input score:91
Input score:92
Input score:86
Input score:97
Input score:-1 Total students are 5 Enter 1 to sort in ascending order, Enter 2 to sort in descending order:2 Data items in original order 90 91 92 86 97 Data items in descending order 97 92 91 90 86

 

 

// L9-9

#include <stdio.h> 
#define N 40 
int ReadScore (int Score []); 
void PrintScore (Score int [], int n-);                      
void SelectionSort (A int [], n-int, int (* Compare) (int A, int B)); 
int Ascending (int A, int B);  int Descending (int A, int B);  void Swap (int * X, int * Y); int main () {int Score [N], n; int order; / * value of 1 indicates the ascending order, a descending sort is represented by 2 * / n-ReadScore = (score); / * score input, returns the number of students * / the printf ( "% D are the Total students. \ n-" , n-); the printf ( "in the Enter Sort Ascending. 1 to Order, \ n-" ); the printf ( "2 to the Enter Sort Descending Order in:" ); Scanf ( "% D", &Order); the printf ( "in the Data items Original Order \ n-" ); PrintScore (Score, n-); / * output before sorting results * / IF (Order ==. 1 ) {SelectionSort (Score, n-, Ascending); / * function pointer to Ascending () * / the printf ( "the Data items in Ascending Order \ n-" );} the else {SelectionSort (Score, n-, Descending); / * function pointer to Descending () * / the printf ( "the Data items in Order Descending \ n-" );} PrintScore (score, n-); / * score outputting sorted * / return 0 ;} / * function: input students in a course of performance, when the negative input, the input ends, returns the number of students * / int ReadScore (int Score []) {int I = -1 ; do {I ++ ; the printf ( "the Input Score:" ); Scanf ( "% D", & Score [I]);} the while ( score [I]> = 0 ); return I;} / * function: output student performance * / void PrintScore (int score [ ],int n) { int* y = temp; }

 

 

void SelectionSort(int a[], int n, int (*compare)(int a, int b))

Tells the compiler function SelectionSort compare the parameter is a pointer, the pointer can point to a two integer variable parameter, the function returns the integer value

The function pointer can point to either function Ascending (), the function may be directed Descending ()

It is possible with such a general sort function, ascending or descending order to achieve

 

Although the definition of a function pointer did not specify which of the pointer to the function, but in the program through the assignment of function pointers point to different functions, in order to achieve a different point in the calling function

Programming using function pointers help to improve the versatility, simplicity of procedures, reduce the duplication code

 

Guess you like

Origin www.cnblogs.com/dingdangsunny/p/11372728.html