gpt4 book ai didi

c - 从 C 中的斐波那契数列返回特定数字

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:14 25 4
gpt4 key购买 nike

我正在编写一个计算斐波那契数列中特定数字的 C 程序,尽管我无法将序列作为数组返回....

我做错了什么?

int fibonacci(int ceiling){  int counter;  int num1 = 1, num2 = 1;  static int fibArray[1000];  for (counter = 1; counter < ceiling; counter+=2)    {      fibArray[counter] = num1;      fibArray[counter+1] = num2;      num2 += num1;      num1 += num2;    }  return &(fibArray);}

我也得到了错误:

fibonacci.c:28: warning: return makes integer from pointer without a cast

?

最佳答案

由于您要返回一个指向数组的指针,因此返回类型应该是 int*。这是示例代码:

int* fibonacci(int ceiling) //Modified return type
{
int counter;
int num1 = 1, num2 = 1;
static int fibArray[1000];
for (counter = 1; counter < ceiling; counter+=2)
{
fibArray[counter] = num1;
fibArray[counter+1] = num2;
num2 += num1;
num1 += num2;
}
return (fibArray); //Return the address of the array's starting position
}

关于c - 从 C 中的斐波那契数列返回特定数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5131818/

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