当前位置:网站首页>C language pointer and array - learning 23

C language pointer and array - learning 23

2022-06-12 00:39:00 XG. Solitary dream

This paper is finally updated at 2022 year 02 month 17 Japan , Has more than 11 I didn't update it . If the article content or picture resources fail , Please leave a message for feedback , I will deal with it in time , thank you !

Pointer variables as function parameters

  • The parameters of a function can be not only integers 、 floating-point 、 Data such as character type , It can also be a pointer type .
  • Its function is to The address of one variable is passed to another function .
  • for example :void swap(int *a,int *b)
#include <stdio.h>

void main() {
    void swap(int *x, int *y);
    int a,b;
    int *a1 = &a;
    int    *b1 = b;

    printf(" Please enter a,b Value :\n");
    scanf_s("%d %d", &a, &b);

    if (a < b) {
        swap(a1, b1);
    }
    printf("max = %d, min=%d\n", a, b);
}

void swap(int *x, int *y) {
    int p;
    p = *x;
    *x = *y;
    *y = p;
}
  • Be careful : Function call Sure ( And only ) obtain A return value ( Functional value ), While using Pointer to the variable As a parameter , Sure Get multiple changed values . It's hard to do this without pointer variables .
  • If you want to get n Values to change
    • 1. stay In the main function, there is n A variable , use n Pointer variables point to them ;
    • 2. Design a function , Yes n Pointer parameters . In this Function to change this n Values of parameters ;
    • 3. Call this function in the main function. , When called, this n Pointer variables as arguments , Put their Address Pass it on to The formal parameter of the function ;
    • 4. During the execution of this function , Pointer variable through parameter , Change what they point to n Values of variables ;
    • 5. You can use these variables that change the value in the main function .
  • Example

Input 3 It's an integer a,b,c, It is required to output them in the order of large to small . Implement with function .

#include <stdio.h>

void main() {
    void exchange(int *x, int *y,int *z);
    int a,b,c;
    int *a1 = &a;
    int    *b1 = &b;
    int *c1 = &c;
    printf(" Please enter a,b,c Value :\n");
    scanf_s("%d %d %d", &a, &b, &c);
    exchange(a1,b1,c1);
    printf("%d, %d, %d\n", a,b,c);
}

void swap(int *x, int *y) {
    int p;
    p = *x;
    *x = *y;
    *y = p;
}

void exchange(int *x, int *y, int *z) {
    void swap(int *x, int *y);
    if (*x < *y) {
        swap(x, y);
    }
    if (*x < *z) {
        swap(x, z);
    }
    if (*y < *z) {
        swap(y, z);
    }
}

Pointer to array element

  • A variable has an address , An array contains several elements , Each array element has a corresponding address .
  • Pointer variables can point to array elements ( Put the address of an element into a pointer variable ).
  • The pointer to the array element is the address of the array element .

Operation of pointer when referring to array elements

  • When the pointer points to an array element , The following operations are allowed :
    • One plus an integer ( use + or +=), for example :p + 1
    • One minus an integer ( use - or -=), for example :p - 1
    • Subtract oneortwo pointers , Such as p1-p2 ( Only p1 and p2 All points to elements in the same array )
  • If the pointer variable p Pointed to an element in the array
    • p+1 Point to the next element in the same array ;
    • p-1 Points to the previous element in the same array .
  • 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 .
  • for example
#include <stdio.h>

void main() {
    int a[5] = {1,2,3,4,5};
    int *a1 = &a[0]; //  perhaps  int *a1 = a
    printf("%d, %d\n", *a1, a1);
    a1 = a1 + 2;
    printf("%d, %d\n",*a1, a1);
}
  • When p Point to a When the first element of the array , Array elements a[i] There are four kinds of address expressions for :
    • &a[i]
    • a + i
    • p + i
    • &p[i]
  • When p Point to a When the first element of the array , Array elements a[i] There are four kinds of value expressions for :
    • a[i]
    • *(a + i)
    • *(p + i)
    • p[i]
  • a and p There are essential differences :
    • a It's a pointer constant , Its value is immutable ;
    • p It's a pointer variable , Its value is variable .
    • therefore ,a++、a = p And so on are illegal expressions ;
    • and p++、p = a They're all legal expressions .
  • If The pointer p1 and p2 All point to the same array
    • p2 - p1 To calculate p2 The element referred to And p1 The element referred to How many elements are there between .
    • Subtract two pointer variables The meaning is The difference between the two addresses Divide Number of bytes per array element .
  • Example
#include <stdio.h>

void main() {
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    int *p1 = &a[0];
    int *p2 = &a[6];
    printf("p1 The address of :%d\n",p1);
    printf("p2 The address of :%d\n",p2);
    printf("%d\n", p2 - p1);
    printf("%d\n",*p2 - *p1);  // int  by 4 Bytes 
}
  • *++p, Equivalent to *(++p), First of all p+1 namely Point to the next group element , Retake *p As the value of this expression .
  • *p++, Equivalent to *(p++), take p As such Value of expression , Then make p+1.
  • (*p)++, First p The point is Element value As The value of this expression , then The element value plus 1.
  • ++(*p), take p The point is Element value plus 1 As The value of this expression .
#include <stdio.h>

void main() {
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    int *p = &a[0];
    printf("%d\n",*p);
    printf("%d\n",*++p);
    printf("%d\n",*p++);
    printf("%d\n",(*p)++);
    printf("%d\n",++(*p));
}

Reference array elements through pointers

  • Reference an array element , You can use the following two methods :
    • 1. The subscript method , for example :a[i]
    • 2. Pointer to the method , for example :*(a+i) or *(p+i) among a It's an array name ,p Is a pointer variable to an array element , Its initial value p = a
  • Example

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

#include <stdio.h>

void main() {
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    int *p;

    //  The subscript method 
    for (int i = 0; i < 10; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");

    //  Calculating array element address by array name 
    for (int i = 0; i < 10; i++) {
        printf("%d ", *(a+i));
    }
    printf("\n");

    //  Pointer to the method 
    for (p=a; p <a+10; p++) {
        printf("%d ", *p);
    }
    printf("\n");
}
  • 3 Comparison of two methods :
    • The first (1) And the (2) Methods Same execution efficiency
      • C Compiling system is to a[i] Convert to *(a+i) To deal with the , That is, calculate the element address first .
      • Therefore, the (1) And the (2) Two ways to find array elements More time-consuming .
    • 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 .
      • such Change address values regularly for example p++ Can greatly improve the efficiency of implementation .
    • Using subscript method intuitive , Can directly know the number of elements .
    • It is not intuitive to use address method or pointer variable method , It is difficult to quickly determine which element is currently being processed .
原网站

版权声明
本文为[XG. Solitary dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011445163738.html