C 函数返回数组
C 语言中的函数帮助程序员适应模块化程序设计。函数可以定义为接受一个或多个参数,并可以向调用环境返回单个值。此外,函数也可以定义为返回一个数组。在 C 语言中,可以通过以下方法之一使函数返回数组 -
- 将数组作为参数传递并返回指针
- 在函数中声明静态数组并返回其指针
- 使用 malloc() 函数
将数组嵌入结构体变量并将其传递给函数
我们实现这些方法来计算给定数字的平方、立方和平方根。
通过引用传递数组
在下面的示例中,我们在 main() 并将其与一个整数一起传递给 函数。在函数内部,数组将填充平方、立方和平方根。函数返回该数组的指针,main() 函数将使用该指针访问并打印这些值。
示例
#include <stdio.h> #include <math.h> int arrfunction(int, float *); int main(){ int x=100; float arr[3]; arrfunction(x, arr); printf("Square of %d: %f ", x, arr[0]); printf("cube of %d: %f ", x, arr[1]); printf("Square root of %d: %f ", x, arr[2]); return 0; } int arrfunction(int x, float *arr){ arr[0]=pow(x,2); arr[1]=pow(x, 3); arr[2]=pow(x, 0.5); }
输出
Square of 100: 10000.000000 cube of 100: 1000000.000000 Square root of 100: 10.000000
返回静态数组
与其在 main() 函数中传递空数组,不如在被调用函数内部声明一个数组,用所需的值填充它,并返回其指针。但是,返回局部变量的指针是不可接受的,因为它指向的变量已经不存在了。请注意,局部变量在函数作用域结束后就会消失。因此,我们需要在被调用函数(arrfunction)内部使用静态数组,并将其指针返回给 main() 函数。
示例
#include <stdio.h> #include <math.h> float * arrfunction(int); int main(){ int x=100, i; float *arr = arrfunction(x); printf("Square of %d: %f ", x, *arr); printf("cube of %d: %f ", x, arr[1]); printf("Square root of %d: %f ", x, arr[2]); return 0; } float *arrfunction(int x){ static float arr[3]; arr[0]=pow(x,2); arr[1]=pow(x, 3); arr[2]=pow(x, 0.5); return arr; }
输出
Square of 100: 10000.000000 cube of 100: 1000000.000000 Square root of 100: 10.000000
现在,考虑以下函数,它将生成 10 个随机数并以数组形式返回它们,并按如下方式调用此函数 -
示例
#include <stdio.h> #include <time.h> #include <stdlib.h> /* 用于生成和返回随机数的函数 */ int * getRandom( ) { static int r[10]; int i; /* 设置种子 */ srand( (unsigned)time( NULL ) ); for ( i = 0; i < 10; ++i) { r[i] = rand(); printf( "r[%d] = %d ", i, r[i]); } return r; } /* main 函数调用上面定义的函数 */ int main () { /* 指向 int 的指针 */ int *p; int i; p = getRandom(); for ( i = 0; i < 10; i++ ) { printf( "*(p + %d) : %d ", i, *(p + i)); } return 0; }
当上述代码一起编译并执行时,它会产生以下结果 -
输出
r[0] = 2110147662 r[1] = 1427553496 r[2] = 1243625529 r[3] = 857484361 r[4] = 513293736 r[5] = 964923407 r[6] = 36104419 r[7] = 1248464892 r[8] = 1838450240 r[9] = 2096489563 *(p + 0) : 2110147662 *(p + 1) : 1427553496 *(p + 2) : 1243625529 *(p + 3) : 857484361 *(p + 4) : 513293736 *(p + 5) : 964923407 *(p + 6) : 36104419 *(p + 7) : 1248464892 *(p + 8) : 1838450240 *(p + 9) : 2096489563
使用 malloc() 函数
malloc() 函数作为库函数包含在 stdlib.h 头文件 中。它在程序运行时动态分配一块内存。正常的变量声明会导致在编译时分配内存。
void *malloc(size_t size);
malloc() 函数返回一个通用的 void 指针。要在分配的内存中赋值某种数据类型,必须将其类型转换为所需的类型。例如,要存储 int 数据,必须将其类型转换为 int *,如下所示 -
int *x = (int *)malloc(sizeof(int);
让我们分配一块足够的内存来存储三个浮点值,分别对应一个数的平方、立方和平方根,并将浮点指针返回到 main() 函数,计算结果将显示在该指针中。
示例
#include <stdio.h> #include <math.h> #include <stdlib.h> float * arrfunction(int); int main(){ int x=16, i; float *arr = arrfunction(x); printf("Square of %d: %f ", x, arr[0]); printf("cube of %d: %f ", x, arr[1]); printf("Square root of %d: %f ", x, arr[2]); return 0; } float *arrfunction(int x){ float *arr = (float *)malloc(3*sizeof(float)); arr[0]=pow(x,2); arr[1]=pow(x, 3); arr[2]=pow(x, 0.5); return arr; }
输出
Square of 16: 256.000000 cube of 16: 4096.000000 Square root of 16: 4.000000
在结构体中使用数组元素
在此方法中,我们将声明一个结构体,其中包含一个浮点数组作为其元素。被调用函数 (myfunction) 声明一个结构体变量,将平方、立方以及其接收参数的平方根填充到数组元素中,并将其返回给 main() 函数。
示例
#include <stdio.h> #include <math.h> struct mystruct{ float arr[3]; }; struct mystruct myfunction(int x); int main(){ int x = 9; struct mystruct s = myfunction(x); printf("Square of %d: %f ", x, s.arr[0]); printf("cube of %d: %f ", x, s.arr[1]); printf("Square root of %d: %f ", x, s.arr[2]); return 0; } struct mystruct myfunction(int x){ struct mystruct s1; s1.arr[0]=pow(x,2); s1.arr[1]=pow(x,3); s1.arr[2]=pow(x, 0.5); return s1; }
输出
Square of 9: 81.000000 cube of 9: 729.000000 Square root of 9: 3.000000
从函数返回字符串
使用相同的方法,您可以向函数传递并返回字符串。C 语言中的字符串是一个 char 类型的数组。在下面的示例中,我们将字符串与指针一起传递,在函数内部对其进行操作,然后将其返回给 main()。
被调用函数内部有一个本地字符串。传递的字符串会与本地字符串连接起来,然后再返回。
示例
#include <stdio.h> #include <string.h> #include <stdlib.h> char * hellomsg(char *); int main(){ char * name = "TutorialsPoint"; char *arr = hellomsg(name); printf("%s ", arr); return 0; } char * hellomsg(char *x){ char *arr = (char *)malloc(50*sizeof(char)); strcpy(arr, "Hello "); strcat(arr, x); return arr; }
输出
Hello TutorialsPoint