Function returns array (one-dimensional and two-dimensional) in C language

Function return array in C language is a very important application. Sometimes calling a function in a program to return an array can make it easier to achieve certain operations we want, such as returning multiple values ​​at once. What this article brings is Examples of functions returning one-dimensional arrays and two-dimensional arrays in C language.


The function returns a one-dimensional array

What needs to be noted when the function returns a one-dimensional array is: when defining a function, you need to add a * sign after the function type; the one-dimensional array defined in the function body needs to add the keyword static in front of its type; defined in the function body The length of the array needs to be determined.
The C code for a simple function that returns a one-dimensional array is as follows.

# include <stdio.h>

int* return_array ()  // 在定义返回一维数组的函数时,在定义的函数类型后要加*号
{
    
    
    static int a[10];  //这里一定要加上static,这样数组a就是一个静态数组了,数组长度需要再这里确定
    for(int i=0; i<10; i++)
        a[i] = i;
    return a;
}

int main ()
{
    
       
    int *array;
    array = return_array(); //调用函数
    for(int i=0; i<10; i++)
        printf("array[%d] = %d\n",i,array[i]);
}

The most critical part of the above code is the part that defines the array in the function body. Why should we define the type of the array as a static array, that is, add the static keyword in front of its type. This is because the life cycle of a static array runs through the entire program from run to end, and if you do not add the static keyword, then the array you define in the function body is a local variable, so the return value of the above function is the local variable a After the function is called, the value in array a no longer exists, so the value stored in the array cannot be obtained by calling the function in the main function.
The result of running the above code is shown in the figure below.
Insert image description here
As you can see, it is correct to call the function in the main function to print the array value defined in the function body!
If the keyword static is not added when defining the array, the output result is as shown below.
Insert image description here
A warning appears as a result of the operation: the function returns the address of the local variable, so each value in the array is not output correctly.


The function returns a two-dimensional array

What you need to pay attention to when the function returns a two-dimensional array is: when defining a function, you need to add two * signs after the function type; the two-dimensional array defined in the function body needs to be assigned row length and column length in sequence; The length of the dimensional array needs to be determined.
The C code for a simple function that returns a two-dimensional array is as follows.

# include <stdio.h>
# include <stdlib.h>

int** return_array ()
{
    
    
    int** a; 
    a = (int**)malloc(5*sizeof(int*));  //分配行长度
    for(int i=0; i<5; i++)
        a[i] = (int*)malloc(5*sizeof(int));  //分配列长度

    for(int i=0; i<5; i++)   //给数组赋初值
        for(int j=0; j<5; j++)
            a[i][j] = i*10+j;
    return a;
}

int main ()
{
    
       
    int **array;
    array = return_array(); //调用函数
    for(int i=0; i<5; i++)
    {
    
    
        for(int j=0; j<5; j++)
            printf("array[%d][%d] = %2d  ",i,j,array[i][j]);
        printf("\n");
    }     
}

The result of running the above code is shown in the figure below.
Insert image description here
Some people may wonder why the row length and column length are not directly given in square brackets when defining a two-dimensional array, as shown below.
Insert image description here
But after defining it this way, the output will warn us that the array we defined is incompatible with the return type.


The above is everything about functions returning one-dimensional arrays and two-dimensional arrays in C language!
Reference article for this article:
How to correctly return an array in C language function?
Transfer of two-dimensional array/two-dimensional array as return value of function

Guess you like

Origin blog.csdn.net/weixin_42570192/article/details/131203554