Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to return an array in C language?
How to return an array in C language?

In C language, an array cannot be returned directly, but an array can be returned by returning a pointer of the corresponding type.

In most cases, one-dimensional arrays and one-dimensional pointers are interchangeable.

For example, define a function to apply for a dynamic array of integers of a certain length. The length is passed in as a parameter and the result is returned. If an error occurs, the null pointer NULL is returned. The code can be written in the following form: int?*int_array_create(int?n)//The parameter n is the number of array elements

{

int?*r;?

< p>if(n<=0)?return?NULL;//Parameter error

r?=?(int?*)malloc(sizeof(int)*n);//Apply for memory space, The size is n int lengths.

return?r;//Return the pointer of the obtained integer array.

?

}