C language entry to C ++ master: C language entry function (4)

Chapter 7 Functions

Section 4 Arrays as function parameters

1. Array elements as function arguments

#include <iostream>

int whichmax(int x, int y) {
    return (x > y ? x : y);
}

int main() {
    std::cout << "Hello, World!" << std::endl;
    //相当于定义了10个变量,a[0]-a[9]
    int a[10];
    a[0] = 100;
    a[1] = 12;
    int res = whichmax(a[0], a[1]);
    printf("whichmax res: %d\n", res);
    return 0;
}

2. Array name as function argument

  • The number of actual and formal parameters must be equal, the types must be consistent, correspond in order, and pass one by one

  • The data transmission of actual parameter variables to formal parameter variables is value transmission, that is, single-term transmission, only the actual parameters are transmitted to the formal parameters, and the actual parameters cannot be transmitted by the formal parameters

  • The array name can be used as the actual parameter of the function. The array name represents the first address of the array. When the data name is passed as the actual parameter of the function, the first address of the array is passed at this time.
    At this time, the formal parameters in the function should also be used Array name (can also be array pointer)

  • When the array name is used as a function parameter, it is not a value transfer or a single item transfer, but the start address of the real parameter group is passed to the shape parameter group,
    so that the two arrays will occupy a section of memory together. Memory is allocated), that is to say, if the value of each element in the shape parameter group changes,
    the value of the element in the real parameter group will also change accordingly.This is obviously different from the function parameter of the variable.

Explanation:

  • 1. If the actual parameter is an array name, the formal parameter is also the array name
  • 2. The shape parameter group and real parameter group must be of the same type, for example, both are int type, otherwise you will get unexpected errors.
  • 3. The size of the shape parameter group can not be specified, even if it is formulated, it can be inconsistent with the size of the real parameter group, because the c compiler does not check the size of the shape parameter group,
    but only the first address of the real parameter group is passed to the formal parameter

3. Use multi-dimensional array names as function arguments

  • Multi-dimensional array names can be used as actual and formal parameters. The shape parameter group is defined, you can specify the size of each dimension, you can also omit the size of the first dimension,
    but you can not omit the size of the second dimension

  • The actual and formal parameters should be as consistent as possible, so that the subscripts referenced by the actual and formal parameters can be consistent, reducing the chance of error

#include <iostream>

void changVal(int b[3][4]) {
    b[0][0] = 20;
    b[1][2] = 0;
    b[2][3] = 50;
    return;
}

int main() {
    std::cout << "Hello, World!" << std::endl;
    int a[3][4] = {85, 70, 98, 92, 78, 40, 90, 33, 80, 75, 64, 84};
    changVal(a);

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            printf("the value of a[%d][%d]: %d\n", i, j, a[i][j]);
        }
    }
    return 0;
}
Published 359 original articles · praised 248 · 660,000 views

Guess you like

Origin blog.csdn.net/Felaim/article/details/105666091