C ++指针数组函数按地址传递

时间:2016-12-05 00:25:07

标签: c++ arrays pointers

需要帮助,如何在fibo函数上打印arrfibo []值的arr指针?

#include <iostream>
using namespace std;

int fibo(int input){

    for(int i=0;i<input;i++){
        if(i<2){
            arrfibo[i] = i;
        }else{
            arrfibo[i] = arrfibo[i-2] + arrfibo[i-1];
        }
    }   
}

main(){
    int inp;
    cout << "Enter number of fibonancci = ";
    cin >> inp;
    int *arr;

    // how can i print arr pointer with value of arrfibo[] on fibo function ?   
}

感谢您的回答,

1 个答案:

答案 0 :(得分:1)

int main(){
     int inp;
     cout << "Enter number of fibonacci = ";
     cin >> inp;
     int *arr = new int[inp];
     fibo(inp, arr);
     //Any additional code
     //Delete dynamic memory.
     delete [] arr;
     }

然后将fibo修改为:

void fibo(int input, int* arrfibo){

for(int i=0;i<input;i++){
    if(i<2){
        arrfibo[i] = i;
    }else{
        arrfibo[i] = arrfibo[i-2] + arrfibo[i-1];
    }
}   
 //Use arrfibo at your will after this point.
}