gpt4 book ai didi

c - 如何从 C 函数创建数组返回类型?

转载 作者:太空狗 更新时间:2023-10-29 16:33:09 25 4
gpt4 key购买 nike

我已尝试返回数组名称,如下所示。基本上我试图让函数测试返回一个可以在 main 中使用的数组。你能告诉我我需要阅读更多内容以了解如何执行这样的功能吗?

#include <stdio.h>

int test(int size, int x){
int factorFunction[size];
factorFunction[0] = 5 + x;
factorFunction[1] = 7 + x;
factorFunction[2] = 9 + x;
return factorFunction;
}

int main(void){
int factors[2];
factors = test(2, 3);
printf("%d", factors[1]);
return 0;
}

我收到编译器错误:

smallestMultiple.c:8: warning: return makes integer from pointer without a cast
smallestMultiple.c:8: warning: function returns address of local variable
smallestMultiple.c: In function ‘main’:
smallestMultiple.c:13: error: incompatible types in assignment

最佳答案

函数不能在 C 中返回数组。

但是,它们可以返回结构。并且结构可以包含数组...

关于c - 如何从 C 函数创建数组返回类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14297169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com