How to return an array from a function and loop through it?

前端 未结 7 1643
广开言路
广开言路 2021-01-24 08:12
#include 

int* fib(int);

int main()
{
    int count;
    std::cout<<\"enter number up to which fibonacci series is to be printed\"<

        
7条回答
  •  遥遥无期
    2021-01-24 08:51

    First, you don't have to allocate int *p=new int[count]; inside main, because you will recieve from the fib function a pointer to an already alocated memory.

    Secondly, everything that is after a return statement is unreachable code, so you can either remove it, or move it before return.

    Furthermore, if you delete the array inside fib function, you will return a null pointer.

    And the main problem is at:

    for(i<0;i<=count;i++)
    

    whom correct for is:

    for(i = 0; i <= count; i++)
    

提交回复
热议问题