C language basics: pointers as parameters

Let's first think about such a problem. If there are two variables that need to exchange their values, what should we do? The answer is very simple:

#include <stdio.h>

int main(int argc, char **args)
{
        int i = 3;
        int j = 5;

        int t;

        t = i;
        i = j;
        j = t;

        printf("i = %d\nj = %d\n", i, j);

        return 0;
}


operation result:

i = 5
j = 3


If we have a lot of groups of variables to do value exchange, such as 100 groups of variables, we must not write 100 times of exchange code. The first solution is to write a function swap with a swap function. This swap function receives parameters by way to swap variables:

#include <stdio.h>

void swap(int m, int n)
{
        int t;

        t = m;
        m = n;
        n = t;
}

int main(int argc, char **args)
{
        int i = 3;
        int j = 5;

        swap(i, j);

        printf("i = %d\nj = %d\n", i, j);

        return 0;
}


operation result:

i = 3
j = 5


The running result is wrong, and the values ​​of i and j are not interchanged as we expected. The reason is that m and n are the parameters of the function in void swap(int m, int n), and when the main function executes swap(i, j); m and n just get the values ​​of i and j, but m and n n is not i and j themselves. When m and n are swapped, the values ​​of m and n are indeed swapped, but i and j do not change. For the whole program i, j, m, n are 4 different variables, there is no relationship between them. In order to solve the problem of variable exchange, we need to modify the code and change the parameters of swap to pointer parameters. The modified program is as follows:

#include <stdio.h>

void swap(int *m, int *n)
{
        int t;

        t = *m;
        *m = *n;
        *n = t;
}

int main(int argc, char **args)
{
        int i = 3;
        int j = 5;

        swap(&i, &j);

        printf("i = %d\nj = %d\n", i, j);

        return 0;
}


operation result:

i = 5
j = 3

The result this time is correct. In fact, m, n and i and j are all four unrelated variables, but the crux of the problem is that m and n are both pointer variables. When the swap function is called, the addresses of i and j are changed. Passed into m and n, that is to say, the value of m is the memory address of i, and the value of n is the memory address of j. In the swap code, the values ​​of m and n are not exchanged, but the * Swap m and *n, that is, take the variables in the addresses in m and n and perform the swap operation. The two variables i and j in this address are affected by this operation. So the values ​​of i and j are swapped.

The advantage of using pointers as function parameters is great, it allows the contents of variables outside the function to be manipulated. At the same time, when the parameter of a function is a pointer variable, we should pay special attention when writing the program, because there is a certain risk in modifying the external variable of the function inside the function, and these pointers can only be called as parameters when the function function is clearly known. The function.

Let's take an example of a pointer variable as a parameter. Suppose we want to initialize a 2-dimensional array. This 2-dimensional array has 5 rows and 10 columns. Let's implement 2 functions. init_array can make all the values ​​in the array become The specified value, show_array can display this array:

#include <stdio.h>

#define MAX_ROW     5
#define MAX_COL     10

void init_array(int *p, int val)
{
        for (int i = 0; i < MAX_ROW; i++)
        {
                for (int j = 0; j < MAX_COL; j++)
                {
                        *p = val;
                        p++;
                }
        }
}

void show_array(int *p)
{
        for (int i = 0; i < MAX_ROW; i++)
        {
                for (int j = 0; j < MAX_COL; j++)
                {
                        printf("%d ", *p++);
                }
                printf("\n");
        }
}

int main(int argc, char **args)
{
        int array[5][10];

        init_array(array[0], 0);
        show_array(array[0]);

        printf("\n");

        init_array(array[0], 3);
        show_array(array[0]);

        printf("\n");

        init_array(array[0], 7);
        show_array(array[0]);

        return 0;
}



operation result:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3

7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7


Welcome to the public account: programming aliens

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691165&siteId=291194637