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

前端 未结 7 1749
广开言路
广开言路 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:53

    Several issues with your code

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

    should actually be

    for(i=0;i

    and

    for(j=0;j<=d;j++)
    

    must read

    for(j=0;j

    And remove the line

    delete ar;
    

    since it does not have any effect after the return statement. Additionally you can get rid of the instantiation

    int *p=new int[count];
    

    in main() since this is done in your fib function also. As it stands, you leak the memory you just allocated.

提交回复
热议问题