C language multidimensional array and pointer - learning 24

This article was last updated on February 19, 2022, and has not been updated for more than 8 days. If the article content or picture resources fail, please leave a message and feedback. I will deal with it in time. Thank you!

Array name as function parameter

  • When using the array name as the function parameter, because the array name of the actual parameter represents the address of the first element of the array, the formal parameter should be a pointer variable.
  • C compilation takes the formal parameter array name as a variable.
  • The argument array name is a pointer constant, but the shape parameter group name is treated as a pointer variable.
  • After the function call combines virtual and real, its value is the address of the first element of the argument array.
  • During the execution of the function, the formal parameter array can be re assigned.
  • For example:
void fun (arr[],int n){ 
    printf("%d\n", *arr);  // Value of output a[0]
    arr=arr+2;
    printf("%d\n", *arr);  // Value of output a[2]
}
  • example

Store n integers in array a in reverse order

#include <stdio.h>

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

void inv(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;
    }
}

Using pointer variables as arguments

#include <stdio.h>

void main() {
    void inv(int *x, int n);
    int i,arr[10] = { 1,2,3,4,5,6,7,8,9,10 }, *p;
    p = arr; // If you use a pointer variable as an argument, you must first make the pointer variable have a definite value and point to a defined unit.
    for (i = 0; i < 10; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    inv(p, 10);
    
    for (p = arr; p <arr + 10; p++) {
        printf("%d ",*p);
    }
    printf("\n");
}

void inv(int *x, int n) {
    int temp, *p,*i, *j, m = (n - 1) / 2;
    p = x + m;
    i = x;
    j = x + n - 1;
    for (; i <= p; i++,j--) {
        temp = *i;
        *i = *j;
        *j = temp;
    }
}

Use the pointer method to sort 10 integers from large to small.

#include <stdio.h>
void main(){ 
    void sort(int *x, int n);
    int i, *p, a[10] = {9,8,6,2,3,4,5,1,2,10};
    p = a;
    for (i = 0; i < 10; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
    sort(p, 10);
    for (i = 0; i < 10; i++) {
        printf("%d ",*p); 
        p++;
    }
    printf("\n");
}
void sort(int *x, int n) {
    int i, j, k, t;
    for (i = 0; i < n - 1; i++) {
        k = i;
        for (j = i + 1; j < n; j++) {
            if (*(x+j)>*(x+k)) {
                k = j;
            }
                if (k != i) {
                    t = *(x + i);
                    *(x + i) = *(x + k);
                    *(x + k) = t;
                }
        }
    }
}

2. Multidimensional arrays and pointers

  • When a two-dimensional array is stored in memory, it converts two dimensions into one-dimensional form.
  • The two-dimensional array defined in C language can be regarded as a one-dimensional array, and each element of this one-dimensional array is a one-dimensional array.
  • From the perspective of two-dimensional array, a is the name of two-dimensional array. A represents the first address of the whole two-dimensional array and the first address of row 0 of two-dimensional array, which is equal to 1000. a+1 represents the first address of the first line, which is equal to 1008. The address at the beginning of line i of a+i is: a+i × four × 2.
  • The array name can represent the first address of the array, so a[0] represents the address of 0 rows and 0 columns, that is & A0. The value of a[1] is & A1, and the value of a[2] is & A2.
  • a. A [0], a [1], a [2] itself does not occupy memory, does not store data, and only represents an address.
  • Line element address: a+0, a+1, a+2, etc; The column element address indicates: a[0]+0, a[0]+1, a[0]+2, a[0]+3, and so on
    • Element value: * (a [i] + J) < = > * (* (a + I) + J) < = > a [] I [J]

A [i] < = > * (a + I) equivalent: element address in row I, column 0 & A [i] [0] A [i] + j < = > * (a + I) + j equivalent: element address in row I, column j & A [i] [j] A [1] + 3 < = > * (a + 1) + 3 equivalent: element address in row 1 and column 3 & A [1] [3]

  • Row pointer and column pointer
    • A + I = & A [i] = a [i] = * (a + I) = & A [i] [0], with equal values and different meanings.
    • A + I < = > & A [i], indicating the address at the beginning of line I, pointing to the line
    • A [i] < = > * (a + I) < = > & A [0] [0], indicating the address of the element in row I and column 0, pointing to the column
  • example

Use the pointer to output the value of two-dimensional array elements.

#include <stdio.h>

void main() {
    float f[3][4] = { {0.0, 0.1, 0.2, 0.3},{1.0, 1.1, 1.2, 1.3},{2.0, 2.1 ,2.2, 2.3} };
    float *pf;
    int i; 
    pf = f[0];
    for (i = 0; i < 12; i++) {
        if (i != 0 && i % 4 == 0){
            printf("\n");
        }
        printf("%6.2f", *pf++);
    }
    printf("\n");
}
  • The expression * pf + + is equivalent to * (PF + +), which means taking the value of * pf as the value of the expression, and then increasing pf by 1. Each element in the f array is accessed one by one through the change of PF value.
  • The method of sequentially outputting array elements is simple, while specifying the output array elements requires address calculation.
  • For example, a two-dimensional array is n X m (n is row and M is column). The address of the first element is a[0]
    • Calculation formula of relative position of a[i][j] in the array:
      • i * m + j (m is the number of elements in each line)

a0

a0

a0

a0

a1

a1

a1

a1

a2

a2

a2

a2

  • Calculation of displacement:
    • a[1][1] = 1*4+1 = 5
    • a[2][3] = 2*4+3 = 11
  • If initial value: p=a[0]
    • Then: * (p+1*4+1) = *(p+5) = a[1][1]
    • *(p+2*4+3) = *(p+11) = a[2][3]
    • The array subscript starts from 0 to calculate the relative position

Pointer variable of multidimensional array

  • Two dimensional array pointer variable description
    • The general form is:
    • Type specifier (* pointer variable name) [length]
    • For example: int(*p)[4]
  • After decomposing the two-dimensional array a into one-dimensional arrays a[0],a[1],a[2], Set p as the pointer variable to the two-dimensional array.
  • example

Use the pointer variable pointing to the one-dimensional array to output the value of the two-dimensional array element.

#include <stdio.h>

void main() {
    float f[3][4] = { {0.0, 0.1, 0.2, 0.3},{1.0, 1.1, 1.2, 1.3},{2.0, 2.1 ,2.2, 2.3} };
    float (*pf)[4];
    int i,j; 
    pf = f;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 4; j++) {
            printf("%6.2f", *(*pf+i)+j);
        }
        printf("\n");
    }
}
  • Multidimensional array pointer as function parameter
    • One dimensional array names can be passed as function parameters, and multi-dimensional array names can also be passed as function parameters.
  • example

Three students learn four courses each, calculate the total average score, and output the nth student's score

#include <stdio.h>
void main() {
    void average(float *p, int n);
    void search(float(*p)[4], int n);
    float score[3][4] = { {65, 67, 79, 60}, { 80,87,90,81 }, { 90,99,100,98 } };
    average(*score, 12);
    search(score, 1);
}

void average(float *p, int n) {
    float *p_end, sum = 0, aver;
    p_end = p + n - 1;
    for (; p <= p_end; p ++) {
        sum = sum + (*p);
    }
    aver = sum / n;
    printf("Average score:%6.2f\n", aver);
}
void search(float(*p)[4], int n) {
    int i;
    printf("The first%d Student achievement:\n", n);
    for (i = 0; i < 4; i++) {
        printf("%6.2f ", *(*(p + n) + i));
    }
    printf("\n");
}

Added by henka on Tue, 01 Mar 2022 09:19:55 +0200