严格来说,无法从函数返回一个数组,但可以从函数返回一个指向任何数据结构的指针,包括一个指向数组的指针。
一种方式如下:
|
千万要注意:不能从函数中返回一个指向函数的局部变量的指针。
另一种方式如下:#include <stdio.h>
#include <stdlib.h>
struct tag{
int array[20];
}x, y;
struct tag func(void);
int main(void)
{
int i;
x = func();
for(i = 0; i < 20; i++){
printf("%d/n", x.array[i]);
}
exit(0);
}
struct tag func()
{
int i;
for(i = 0; i < 20; i++){
y.array[i] = i;
}
return y;
}