当前位置:网站首页>My C language learning record (blue bridge) -- on the pointer

My C language learning record (blue bridge) -- on the pointer

2022-07-06 02:48:00 Someone -jojo

The pointer

In order to understand the concept of pointer , Let's start with a short story .

Sherlock Holmes sent Watson to the castle where Sir will lived to retrieve an important data . Day , In the study , Sir will personally locked the data in the bookcase in front of Holmes and Watson, numbered 3010 Drawers , Shine with a flashlight , I saw a note inside , It's spelled 6 A big word : Address 2000. Watson's eyes lit up , Quickly found the number 2000 Drawers , Took out important data 123, Completed the task .

The following figure can be used to describe the relationship between several data .

 

explain

thus it can be seen , Pointer variable is a special variable , It does not store data , But the address of another variable . This variable storing data is called the target variable pointed to by the pointer variable . Because the target variable it points to can be accessed directly through the address in the pointer variable , Pointer variables are often referred to as pointers .

Pointer variables are special variables that store addresses , Its particularity is manifested in type and value . From the perspective of variables , Pointer variables also have elements of variables :

  • Name of pointer variable , The name is the same as that of general variables , follow C Naming rules for languages .
  • The type of pointer variable , Is the type of variable pointed to by the pointer variable , Not their own type .
  • In the environment of the experimental building , Pointer variables occupy memory 8 Bytes .

First analyze a pointer case , Access integer types through pointer variables . establish 10-1.c File and enter the following code :

#include<stdio.h>

int main(){

 int a = 100,b = 10;

 int *point_1,*point_2;     // Define pointer variables to integer data point_1,point_2

 point_1 = &a;                 // Put variables a Address assigned to pointer variable point_1

 point_2 = &b;

 printf("a=%d,b=%d\n",a,b);

 printf("%p,%p\n",point_1,point_2);                     // Output a and b Address in memory

 printf("*point_1=%d,*point_2=%d\n",*point_1,*point_2);    // Output variables a and b Value

 return 0;

}

Enter the following command to compile and run :

gcc -o 10-1 10-1.c

./10-1

The program runs as follows :

 

Be careful

If the pointer prints a negative number , You need to print the type  %d  Adjusted for  %ld  It can be output normally . Adjust to  %p  To match pointer type is best .

Program analysis

  • Two pointer variables are defined at the beginning  point_1  and  point_2. But now they Does not point to any variable , Just provide two pointer variables , Specify that they can point to shaping variables , As for which integer variable to point to , To specify .

Procedure No 6,7 The function of two lines is to make  point_1  Point to a,point_2  Point to b, here  point_1  The value of is &a( namely a The address of ),point_2  The value of is &b.

  • The first 10 Line output  *point_1  and  *point_2  Value , Among them “*” Express Point to .

among  *point_1  Represents a pointer variable  point_1  The variable pointed to , That's the variable a.*point_2  Represents a pointer variable  point_2  The variable pointed to , That's the variable b. From the running results, they are 100 and 10.

  • There are two occurrences in the program  *point_1  and  *point_2, But the two have different meanings .

Procedure No 4 Yes  *point_1  and  *point_2  Indicates that two pointer variables are defined  *point_1  and  *point_2. In front of them “*” It just means that the variable is a pointer variable . At the end of the program 10 In a row  printf  Function  *point_1  and  *point_2  It means pointer variable  point_1  and  point_2  The variable pointed to .

Defining pointer variables

stay 10-1.c We saw the definition of pointer variables , The general form of defining a pointer variable is :

Type name * Pointer variable name

Such as :

int * point_1,* point_2;

The base type defined above is int Pointer variable for  point_1  and  point_2  Can only be used to point to integer variables .

Be careful

Let's emphasize again here , When defining pointer variables, pay attention to , Before pointer variable “*” Indicates that the variable is of pointer type . The pointer variable name is  point_1  and  point_2, instead of  *point_1  and  *point_2. This is different from defining integer or real variables . Above program 6 Row sum 7 Line cannot be written  * point_1=&a;  and  *point_2=&b;  Of . because a The address of is assigned to the pointer variable  point_1, Instead of assigning to  *point_1( Namely variable a).

Reference pointer variables

When referring to pointer variables , There are the following 3 In this case :

1. Assign value to pointer variable . Such as :

p=&a;  // hold a Address assigned to pointer variable p

Refer to the variable pointed to by the pointer variable .

2. If it has been implemented  p=&a;  That is, pointer variable p Pointed to an integer variable a, be

printf("%d",*p);

Its function is to output pointer variables in the form of integers p The value of the variable pointed to , Namely variable a Value .

If there is the following assignment statement :

*p = 1;

Indicates that the integer 1 Assign to p The variable currently pointed to , If p Point to variable a, It is equivalent to 1 Assign to a, namely  a=1;.

Reference to the value of a pointer variable . Such as :

printf("%o",p);

The function is to output pointer variables in the form of octal numbers p Value , If p Yes a, That's the output a The address of , namely &a.

Be careful

Compare address operators & And pointer operators * The difference of :

 

Enter two integers , Output in the order of big first and then small a and b.

Their thinking

Use the pointer method to deal with this problem . Do not exchange values of integer variables , Instead, exchange the values of two pointer variables .

Write the source program 10-2.c

#include<stdio.h>

int main(){

    int *p1,*p2,*p,a,b;

    printf("please enter two integer number:");

    scanf("%d,%d",&a,&b);

    p1 = &a;

    p2 = &b;

// Be careful : Here is the value of the exchange pointer

// That is, the pointer has changed

// But two variables a and b The value of is unchanged

    if(a<b){

        p = p1;

        p1 = p2;

        p2 = p;          // send p1 and p2 The value of the swap

    }

// If a < b, Through the results, we can observe p1 Point to b,p2 Point to a

    printf("a=%d,b=%d\n",a,b);

    printf("max=%d,min=%d\n",*p1,*p2);

    return 0;

}

The program runs as follows :

 

Program analysis

Input 23 and 42, because a<b, take p1 and p2 In exchange for , The situation before and after the exchange is shown in the figure below :

 

Be careful

  • a and b The value of is not swapped , They still keep their original value , but p1 and p2 The value of .p1 The value of used to be &a, Later it became &b. So output  *p1  and  *p2  when , Actually output variables b and a Value , So first output 42, Then the output 23.
  • In the program  p=p1;p1=p2;p2=p;  This statement is the method we used before , The value exchange of two variables uses the third variable . In fact, we can use a more concise method to put  p=p1;p1=p2;p2=p;  Change it to  p1=&b,p2=&a, In this way, there is no need to define intermediate variables p, Make the program more concise .

Pointer variables as function parameters

The parameters of a function can be more than just an integer , Floating point data , It can also be a pointer type . Its function is to transfer the address of a variable to another function .

Here is an example : Required functions and 10-2.c identical , But this time we use functions to deal with , And use pointer type data as function parameters .

Write the source program 10-3.c

#include<stdio.h>

int main(){

    void swap(int * point_1,int * point_2);

    int *p1,*p2,a,b;

    printf("please enter two integer number:");

    scanf("%d,%d",&a,&b);

    p1 = &a;

    p2 = &b;

    if(a<b)

        swap(p1,p2);

// If a < b, that a and b The values of are exchanged

// If you print *p1 and *p2 We will find that ,p1 Still pointing a,p2 Still pointing b

    printf("max=%d,min=%d\n",a,b);

    return 0;

}

void swap(int * point_1,int * point_2){

    int temp;

// Be careful : Here is the value of the variable referred to by the exchange pointer

// That is, the pointer has not changed

// But variables a and b The value of

    temp = * point_1;                 // send *p1 and *p2 swap

    *point_1 = * point_2;

    *point_2 = temp;

}

The program runs as follows :

 

Program analysis :

swap  Is a user-defined function , Its function is to exchange two variables (a and b) Value .

swap  Two formal parameters of a function point_1 and point_2 It's a pointer variable . Program runtime , Execute first  main  function , Input a and b Value ( What we input is 23 and 34), And then a and b The addresses of are assigned to int Pointer to the variable p1 and p2, send p1 Point to a,p2 Point to b, See the picture below a, Then perform  if  sentence , because a<b, So execute  swap  function .

Pay attention to the arguments p1 and p2 It's a pointer variable , On function call , Pass the value of the argument variable to the formal parameter variable , What has been taken is still Value passed The way . Therefore, the formal parameters after the combination of deficiency and reality point_1 The value of is &a,point_2 The value of is &b, See the picture below b. At this time p1 and point_1 All point to variables a,p2 and point_2 All point to variables b. Then perform  swap  The body of a function , send * point_1 and * point_2 The value of the swap , That is to make a and b The value of the swap . The situation after exchange is shown in Figure c.

After the function call , Shape parameter point_1 and point_2 No longer exists ( Released ), As shown in the figure d, Last in  main  Output in function a and b The value of is already an exchanged value .

 

You can see , In execution  swap  After the function , Variable a and b The value of .

Be careful : Please analyze it carefully , How did this change happen , This change is not realized by passing back the parameter value . You can consider whether it can be realized through the following functions a and b swap .

void swap(int x ,int y){

    int temp;

    temp = x;

    x = y;

    y = temp;

}

If in  main  Call in function  swap  function :swap(a,b);  What will happen ?

On function call ,a The value of is transmitted to x,b The value of is transmitted to y, After execution  swap  After the function ,x and y The values of are interchangeable , But it did not affect a and b Value .

At the end of the function , Variable x and y The release of the ,main  Function a and b Not interchangeable . in other words , This one-way value transfer , A change in the value of a formal parameter cannot change the value of an argument .

In order to make the variable value changed in the function be called by the main function  main  used , You cannot take the above method of taking the variable to be changed as a parameter , Instead, you should use pointer variables as function parameters , In the process of function execution, the value of the variable pointed by the pointer variable changes , After the function call , These variable values remain .

If you want to get n Values to change , You can do this :

  1. Set in the main function n A variable , use n Pointer variables point to them ;
  2. Design a function , Yes n Pointer parameters , Change... In this function n Values of parameters ;
  3. Call this function in the main function. , When called, this n Pointer variables as arguments , Pass their addresses to the formal parameters of the function ;
  4. During the execution of this function , Pointer variable through parameter , Change what they point to n Values of variables ;
  5. These variables that change the value can be used in the main function .

Is everyone a little dizzy , You write the program just now twice , Write the following program exercise again and think independently. You absolutely master . Input 3 It's an integer a,b,c It is required to output them in descending order , Implement with function .

Their thinking :

use 10-3.c The method of is changed in the function 3 Values of variables . use  swap  Function to exchange the values of two variables , use  exchange  Functions change 3 Values of variables .

Write the source program 10-4.c

#include<stdio.h>

int main(){

    void exchange(int * q1,int * q2,int * q3);

    int a,b,c,*p1,*p2,*p3;

    printf("please enter 3 integer number:");

    scanf("%d%d%d",&a,&b,&c);

    p1 = &a;

    p2 = &b;

    p3 = &c;

    exchange(p1,p2,p3);

    printf("the order is :%d,%d,%d\n",a,b,c);

    return 0;

}

void exchange(int *q1,int *q2,int *q3){

    void swap(int *p1,int *p2);

    if(* q1<* q2) swap(q1,q2);

    if(* q1<* q3) swap(q1,q3);

    if(* q2<* q3) swap(q2,q3);

}

void swap(int *p1,int *p2){

    int temp;

    temp = *p1;

    *p1 = *p2;

    *p2 = temp;

}

The program runs as follows :

 

Please draw a picture similar to our previous example , Carefully analyze the process of variable value change , Don't be lazy !

Reference array by pointer

You can use a pointer variable to point to an array element . for example :

int a[10]={1,2,3,4,5,6,7,8,9,10};

int *P;

p = &a[0];

The above is to make the pointer variable p Point to a The first of an array of 0 Element number .

stay C In language , Array name ( Excluding shape parameter group name , The formal parameter array does not occupy the actual memory unit ) Represents the first element in the array ( That is, the serial number is 0 The elements of ) The address of . therefore , The following two statements are equivalent :

p = &a[0];  //p The value of is a[0] The address of

p = a;      //p Is the value of an array a First element ( namely a[0]) The address of

Be careful :

The array name does not represent the entire array , Address representing only the first element of the array . Above  p=a;  The role of is hold a The address of the first element of an array is assigned to a pointer variable p”, instead of Put the array a The value of each element is assigned to p”.

Operate on array elements through pointers

Program examples

When the pointer points to an array element , You can perform the following operations on the pointer :

  • Add or subtract an integer , Such as  p+1;  perhaps  p-1;
  • Self addition operation , Such as  p++  perhaps  ++p
  • Self subtraction , Such as  p-- perhaps  --p

They are described as follows :

  1. If the pointer variable p Pointed to an element in the array , be p+1 Point to the next element in the same array ,p-1 Points to the previous element in the same array . Be careful : perform p+1 It's not going to be p Value ( Address ) Simply add 1, It's the number of bytes taken up by an array element . for example , Array elements are float type , Each element accounts for 4 Bytes , be p+1 Means to make p The value of the add 4 Bytes , So that it points to the next element .p+1 The address represented is actually p+1*d,d Is the number of bytes occupied by an array element . if p The value of is 2000, be p+1 The value is not 2001 It is 2004.
  2. If p The initial value for the &a[0], be p+i and a+i It's an array element a[i] The address of , Or say , They point to a The array number is i The elements of , See the picture below , Here's the thing to notice a Address representing the first element of the array ,a+1 It's also the address , Its calculation method is the same as p+1, That is, its actual address is a+1*d. for example ,p+9 and a+9 The value of is &a[9], It points to a[9].

     3.*(p+i) and *(a+i) yes p+i or a+i Element pointed to , namely a[i]. for example *(p+5) and *(a+5) Namely a[5], The three are equivalent .

 

There is an integer array a, Yes 10 Elements , Require all elements in the output array .

Write the source program 10-5.c

#include<stdio.h>

int main(){

    int a[10];

    int i;

    printf("please enter 10 integer numbers: ");

    for(i=0;i<10;i++)

        scanf("%d",&a[i]);

    for(i=0;i<10;i++)

        printf("%d\t",a[i]);     // Array elements are represented by array names and Subscripts

    return 0;

}

Write the source program 10-6.c

#include<stdio.h>

int main(){

    int a[10];

    int i;

    printf("please enter 10 integer numbers: ");

    for(i=0;i<10;i++)

        scanf("%d",&a[i]);

    for(i=0;i<10;i++)

        printf("%d\t",*(a+i));  // Calculate the element address through the array name and element sequence number , Find this element again

    return 0;

}

Write the source program 10-7.c

#include<stdio.h>

int main(){

    int a[10];

    int *p,i;

    printf("please enter 10 integer numbers: ");

    for(i=0;i<10;i++)

        scanf("%d",&a[i]);

    for(p=a;p<(a+10);p++)

        printf("%d\t",*p);  // Pointer to the current array element

    return 0;

}

Run three programs , The program runs as follows :

 

It can be seen that , The result is the same .

Due to window size limitation , The top command  ./10-5  Not shown in the picture .

Program analysis

  • 10-6.c In program  scanf("%d",&a[i]);  It can be changed to  scanf("%d",a+i);. You can also use a+i Express a[i] The address of .
  • 10-7.c In program  scanf("%d",&a[i]);  It can be changed to  scanf("%d",a+i);. Use pointer variables to represent the current address .

Now let's try !

3 Comparison of two methods :

  • The first 1 Species and 2 The execution efficiency of the two methods is the same .C Compiling system is to a[i] Replace with *(a+i) To deal with the , That is, first calculate the address of the element . Therefore, the 1 And the 2 There are many ways to find array elements .
  • The first 3 Methods 1、 The first 2 Fast method , Point directly to an element with a pointer variable , You don't have to recalculate the address every time , image p++ Such a self adding operation is relatively fast . This regularly changes the address value p++ Can greatly improve the efficiency of implementation .
  • It is more intuitive to use subscripts , Can directly know the number of elements . for example ,a[5] Yes, the sequence number in the array is 5 The elements of ( Note that the serial number is from 0 Count up ). The method of sending address or pointer variables is not intuitive , It is difficult to quickly determine which element is currently being processed . for example , Method 3 in , Analyze pointer variables carefully p The current point of , To determine which element is currently output . Experienced programmers often like to use the first 3 In the form of , use p++ Control , Program concise 、 Efficient . For Xiaobai users, it's better to use No 1 Ways of planting , intuitive , It's not easy to make mistakes. .

When using pointer variables to point to array elements , There are some problems that need attention :

You can point to different elements by changing the value of the pointer variable . for example : Above mentioned 3 One way is to use pointer variables p To point to elements , use p++ send p The value of is constantly changing to point to different elements .

If not p Change the method and use the array name a Will the method of change work ? If the above paragraph 3 Of the program in the method

for(p=a;p<(a+10);p++)

printf("%d\t",*p);

Change it to :

for(p=a;a<(p+10);a++)

printf("%d",*a);

This is not going to work . Because the array name a Address representing the first element of the array , It is a pointer constant , Its value is fixed during the running of the program . since a Is a constant , therefore a++ It's impossible . It is worth noting that although its value cannot be changed , But in the program we can use  a+i  Express  a[i], here  a+i  And  a++  The difference is that it does not modify constants a Value ( When used alone ,a++ Is equivalent to a=a+1, It's right a The value of is modified ).

The pointer variable points to the array element. Note the current value of the pointer variable .

Write the source program 10-8.c

#include<stdio.h>

int main(){

    int i,*p,a[10];

    p=a;

    printf("please enter 10 integer number:");

    for(i=0;i<10;i++)

        scanf("%d",p++);

    for(i=0;i<10;i++,p++)

        printf("%d\t",*p);

    printf("\n");

    return 0;

}

The program runs as follows :

 

Program analysis

Obviously, the output value is not a The value of each element in the array . Inspection and analysis procedures are required .

You may feel that there is nothing wrong with the above procedure , Even if you have been told that there is a problem with this program , Still can't find out what the problem is . The problem is with pointer variables p The direction of . Pointer to the variable p The initial value of a Address of the first element of the array , But after the first for After reading data circularly ,p Pointed to a The end of the array . therefore , In the implementation of the 2 individual for loop ,p The starting value of is not &a[0] 了 , It is a+10. Due to the implementation of article 2 individual for loop , Do it every time p++, therefore p Pointing to a Under the array 10 Storage unit .

The solution to this problem only needs to be found in 2 individual for Add an assignment statement before the loop :

p = a;

send p The initial value of is again equal to &a[0], This result is right .

Inductive analysis

According to the above , Reference an array element , There are two ways :

  1. The subscript method , Such as a[i],p[i] In the form of ; intuitive , It's not easy to make mistakes. .
  2. Pointer to the method , Such as *(a+1) or *(p+i). among a It's an array name ,p Is a pointer variable to an array element , Its initial value p=a.

The members of the traversal array are available :

  1. The subscript method , Such as a[2];
  2. Calculating array element address by array name , Find the value of the element ;
  3. Point to array elements with pointer variables .

Be careful :

p[i],*(p+i) The form of must first make  p = a.

Using pointers to reference array elements , More convenient and flexible , There are many skills . Among professionals, they often like to use some skills , To make the program concise . Let's analyze the following situations .

  1. p++ send p Point to the next element a[1]. Then if you are executing * p, Then you get the next element a[1] Value .

p++;

* p;

  1. because ++ and * Same priority , The combination direction is from right to left , So it's equivalent to *(p++). First reference p Value , Realization *p Arithmetic , And then p Self increasing 1.

*p++;

  1. *(p++) And *(++p) Whether the functions are the same ? The answer must be different . The former is first *p Value , Then make p Add 1. The latter is to make p Add 1, Retake *p.

Use the array name as the function parameter

Program examples

In the array chapter, we introduced that you can use the array name as the parameter of the function . for example :

int main(){

    void fun(int arr[],int n);

    int array[10];

             .

             .

             .

    fun(array,10);      // Use the array name as the parameter of the function

    return 0;

}

void fun(int arr[],int n){

        .

        .

        .

}

array Is the actual parameter array name ,arr Is the shape parameter group name . In the first two verses, we should know , When the array name is used as a parameter , If the values of the elements in the parameter array change , The values of the elements of the array of arguments change . What is the reason for this ? It should not be difficult to understand what we learned from the pointer in the previous section .

Program analysis :

  • Let's first look at the case when the array element is an argument . If you have defined a function , Its prototype is void swap(int x,int y);, Suppose that the function is to combine two formal parameters (x,y) Value exchange of , The following function calls exist swap(a[1],a[2]);. Use array elements a[1] and a[2] As an argument , Just like when using variables as arguments , yes “ Value passed ” The way , take a[1] and a[2] The value of is passed one way to x and y. When x and y When the value of is changed a[1] and a[2] The value of .
  • Let's take another look at the case where the array name is used as a function parameter , The parameter array name represents the address of the first element of the array , The formal parameter is used to receive the address of the first element of the array passed from the actual parameter . therefore , A formal parameter should be a pointer variable . actually C The compilation system treats the formal parameter array name as a pointer variable .
    • for example  fun(int arr[],int n) , When the program is compiled, it will arr Treated as a pointer variable , It's equivalent to putting the function  fun  The first part of the book is written as  fun(int *arr,int n);. The above two expressions are equivalent . When this function is called , The system will be in  fun  Create a pointer variable in the function arr, Used to store the address of the first element of the argument array passed from the main function . When arr After receiving the address of the first element of the argument array ,arr Point to the first element of the array of arguments , That is to point array[0]. therefore *arr Namely array[0],*(arr+1) Namely array[1].
    • We can think of it this way , During a function call , The formal parameter array gets the starting address from the actual parameter array , Therefore, the formal parameter array and the actual parameter array share the same memory unit , During function call , If you change the value of the shape parameter group , That is to change the value of the argument array .

C Language often uses this method to change the value of the argument array by calling a function . Will array a in n Integers are stored in reverse order , See the picture below :

 

Their thinking

take a[0] And a[n-1] exchange , then a[1] and a[n-2] exchange ... Until will a[int (n-1)/2] And a[n-int(n-1)/2-1] exchange . Now use a loop to deal with this problem , Set two Position indication variable ” i and j,i At the beginning of 0,j The initial value for the n-1. take a[i] and a[j] In exchange for , And then there was i The value of the add 1,j The value of the reduction 1, then a[i] and a[j] In exchange for , until i=(n-1)/2 until .

Use a function  change  To realize the exchange . The arguments are array names a, Formal parameters can be array names , Pointer variable names can also be used .

Write the source program 10-9.c

#include<stdio.h>

int main(){

    void change(int x[],int n);

    int i,a[10] = {3,7,9,11,0,6,7,5,4,2};

    printf("the original array:\n");

    for(i=0;i<10;i++)

        printf("%d\t",a[i]);

    printf("\n");

    change(a,10);

    printf("the array has been inverted:\n");

    for(i=0;i<10;i++)

        printf("%d\t",a[i]);

    return 0;

}

void change(int x[],int n){

    int temp,i,j,m=(n-1)/2;

    for(i=0;i<=m;i++){

        j = n - 1 - i;

        temp = x[i];

        x[i] = x[j];

        x[j] = temp;

    }

}

Write the source program 10-10.c

#include<stdio.h>

int main(){

    void change(int *x,int n);

    int i,a[10] = {3,7,9,11,0,6,7,5,4,2};

    printf("the original array:\n");

    for(i=0;i<10;i++)

        printf("%d\t",a[i]);

    printf("\n");

    change(a,10);

    printf("the array has been inverted:\n");

    for(i=0;i<10;i++)

        printf("%d\t",a[i]);

   return 0;

}

void change(int *x,int n){

    int *p,temp,*i,*j,m = (n-1) / 2;

    i=x;j=x+n-1;p=x+m;

    for(;i<=p;i++,j--)

    {

        temp = *i;

        *i = *j;

        *j = temp;

    }

}

Run two programs , The program runs as follows :

 

The results are consistent , In other words, they all realize the function of exchange .

Program analysis

actually 10-9.c We can make some changes to this program to get the program 10-10.c.

Will function  change  Parameter in x Change to pointer variable . The corresponding argument is still the array name a( It's an array a The address of the first element ), Pass it to the formal parameter pointer variable x, At this time x Point to a[0].x+m yes a[m] Address of element . set up j and i as well as p They're all pointer variables , Use them to point to relevant elements .i Initial value of x,j The initial value for the x+n-1, See the picture below . bring *i And *j Exchange is a[i] and a[j] In exchange for .

 

Inductive analysis

If you have an array of arguments , To change the value of an element in an array in a function , The correspondence between arguments and formal parameters is as follows 4 In this case .

Both formal and actual parameters use array names , for example :

int main(){

  int a[10];

  .

  .

  .

  f(a,10);

}

int f(int x[],int n){

  .

  .

  .

}

Due to the formal parameter array name x Received the first element of the argument array a[0] The address of , Therefore, it can be considered that during the function call , Formal parameter array and actual parameter array share a memory unit , This form is easy to understand .

The arguments are array names , Formal parameters are pointer variables . for example :

int main(){

  int a[10];

  .

  .

  .

  f(a,10);

}

void f(int *x,int n){

  .

  .

  .

}

Actual parameters a For array name , Shape parameter x by int * Pointer variable of type , After calling the function , Shape parameter x Point to a[0], namely x=&a[0], adopt x A change in the value of , Can point to a Any element of the array . example 11-2.c It belongs to this kind of .

All arguments are pointer variables . for example :

int main(){

    int a[10],*p=a;

    .

    .

    .

    f(p,10);

    .

    .

    .

}

void f(int *x,int n){

  .

  .

  .

}

Actual parameters p And formal parameter x All are int * Pointer variable of type . First make the argument pointer variable p Pointing to the array a[0],p The value of is &a[0]. And then p The value of is passed to the formal parameter pointer variable x,x The initial value of is also &a[0], adopt x The change of the value of can make x Pointing to the array a Any element of .

Argument is a pointer variable , The formal parameter is the array name . for example :

int main(){

    int a[10],*p=a;

    .

    .

    .

    f(p,10);

}

void f(int x[],int n){

  .

  .

  .

  .

}

Actual parameters p Is a pointer variable , It points to a[0]. The formal parameter is the array name x, Compile the system to x Handle as a pointer variable , Today will be a[0] The address of is passed to the parameter x, send x Point to a[0]. It can also be understood as a formal parameter array x and a Arrays share the same memory unit . In the process of execution, you can make x[i] Change in value of , and x[i] Namely a[i]. such ,main  Function can use the value of the changed array element .

above 4 Methods , In essence, it is the transmission of addresses . among 3 and 4 The two are just different in form , In fact, all formal parameters use pointer variables .

原网站

版权声明
本文为[Someone -jojo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132354082164.html