Dre*_*ker 2 c arrays parameters return function
我试图弄清楚如何从main()中的函数返回一个数组.
我正在使用C语言.
这是我的代码.
#include <stdio.h>
int *initArray(int n){
int i;
int *array[n];
for(i = 0; i < n; i++){
array[i] = i*2;
}
return array;
}
main(){
int i, n = 5;
int *array[n];
array[n] = initArray(n);
printf("Here is the array: ");
for(i = 0; i < n; i++){
printf("%d ", array[i]);
}
printf("\n\n");
}
Run Code Online (Sandbox Code Playgroud)
这是控制台给我的错误:
2.c: In function ‘initArray’:
2.c:8:13: warning: assignment makes pointer from integer without a cast [enabled by default]
array[i] = i*2;
^
2.c:11:3: warning: return from incompatible pointer type [enabled by default]
return array;
^
2.c:11:3: warning: function returns address of local variable [-Wreturn-local-addr]
2.c: In function ‘main’:
2.c:23:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("%d ", array[i]);
^
Run Code Online (Sandbox Code Playgroud)
不可能!我讨厌做菜鸟:(
如果您能提供解释,我将不胜感激!:d
编辑:iharob的答案比我的好.先检查他的答案.
编辑#2:我将尝试解释你的代码错误的原因
考虑你问题中main()的第二行:
int *array[n];
Run Code Online (Sandbox Code Playgroud)
让我们试着向后看.
[n]
Run Code Online (Sandbox Code Playgroud)
说我们有一个包含n个元素的数组.我们不知道这些元素是什么类型以及数组的名称是什么,但我们知道我们有一个大小为n的数组.
array[n]
Run Code Online (Sandbox Code Playgroud)
说你的数组叫做数组.
* array[n]
Run Code Online (Sandbox Code Playgroud)
说你有一个指向数组的指针.被指向的数组称为"数组",其大小为n.
int * array[n];
Run Code Online (Sandbox Code Playgroud)
你说你有一个指向大小为n的整数数组的指针.
在这一点上,你是制作2d数组的3/4方法,因为2d数组包含一个指向数组的指针列表.你不希望这样.
相反,你需要的是:
int * array;
Run Code Online (Sandbox Code Playgroud)
此时,我们需要检查你的函数initArray:
int *initArray(int n){
int i;
int *array[n];
for(i = 0; i < n; i++){
array[i] = i*2;
}
return array;
}
Run Code Online (Sandbox Code Playgroud)
initArray的第二行与第二行main相同.做了
int * array;
Run Code Online (Sandbox Code Playgroud)
现在,这里有一个难以解释的部分.
int * array;
Run Code Online (Sandbox Code Playgroud)
不为数组分配空间.在这一点上,它是一个不起眼的指针.那么,我们如何为数组分配空间呢?我们使用malloc()
int * array = malloc(sizeof(int));
Run Code Online (Sandbox Code Playgroud)
仅为一个整数值分配空间.此时,它更像是一个变量而不是一个数组:
[0]
int * array = malloc(sizeof(int) * n);
Run Code Online (Sandbox Code Playgroud)
为n个整数变量分配空间,使其成为一个数组:
例如n = 5:
[0] [0] [0] [0] [0]
注意:实数组中的值可能是垃圾值,因为与calloc不同,malloc不会将内存清零.0是为了简单起见.
但是,malloc并不总是有效,这就是为什么你需要检查它的返回值:(如果不成功,malloc会使array = NULL)
if (array == NULL)
return NULL;
Run Code Online (Sandbox Code Playgroud)
然后,您需要检查initArray的值.
#include <stdio.h>
#include <stdlib.h>
int *initArray(int n){
int i;
int *array = malloc(sizeof(int) * n);
if (array == NULL)
return NULL;
for(i = 0; i < n; i++){
array[i] = i*2;
}
return array;
}
int main(){
int i, n = 5;
int *array = initArray(n);
if (array == NULL)
return 1;
printf("Here is the array: ");
for(i = 0; i < n; i++){
printf("%d ", array[i]);
}
free(array);
printf("\n\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你不能只返回这样的数组.您需要创建一个动态分配的数组才能执行此操作.另外,为什么你还使用二维阵列?
int array[5];
Run Code Online (Sandbox Code Playgroud)
基本上(不完全)相同:
int * array = malloc(sizeof(int) * 5);
Run Code Online (Sandbox Code Playgroud)
后者更灵活一点,你可以调整使用malloc分配的内存,你可以从函数返回指针,就像我发布的代码一样.但要注意,因为动态内存分配是你不想进入的,如果你还没准备好大量的痛苦和调试:)
另外,free()在你使用它之后已经malloc'd的任何东西,你应该在使用已经分配了它的指针之前检查malloc()的返回值.
感谢iharob提醒我将其包含在答案中