C ++通过引用传递来更改函数内部数组的值

时间:2017-02-13 23:58:58

标签: c++

问题是我的指针始终指向相同的内存地址,这使得它不像我想要的那样。

我希望在函数期间存储数组中的多个值,而不是必须将它们写入main中的数组,方法是将它们作为引用传递,这样我就不必返回。任何人都可以帮我解决问题吗?

这里的代码有效,你会看到我得到相同的地址。

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;



 void creatingarray(int starttemp2, int *arrayc2[], int nlength2, int step2){

    int *newtemp = new int;
    *newtemp = starttemp2;
    //cout << phead;

    cout << *newtemp << "   " << endl;

    for (int i=0; i < nlength2; i++){
        arrayc2[i] = newtemp;
        *newtemp = *newtemp + step2;

        cout << *arrayc2[i] << " " << arrayc2[i] <<" ";
        cout << *newtemp << " "<<endl;
    }

   for (int i=0;i<nlength2;i++)
    cout<< *arrayc2[i] << " ";

 }



int main()
{
  int step;
  int starttemp;
  int endtemp;



 cout << "Geef begin en eind temperatuur in om om te zetten met een bepaalde step";
 cout <<  "step:";
 cin >> step ;
 cout << "begintemperatuur in celsius: ";
 cin >> starttemp;
 cout << "eindtemperatuur in celius: ";
 cin >> endtemp;

 int nlength = (endtemp - starttemp) / step;
 int *arrayc[nlength];



 creatingarray(starttemp, arrayc, nlength, step);



/*
for (int accumulater = 0; accumulater < endtemp)
    cout << startemp;
    temperature S("test");*/

    cout << nlength;

   cout << "CELSIUS" << endl;
    for (int i=0;i<nlength;i++)
    cout<< *arrayc[i] << " ";


    return 0;
}

2 个答案:

答案 0 :(得分:1)

这应该会让你前进。

这可能不是你打算做的,但你打算做的事情还不清楚。

#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

int main()
{
    int step;
    int starttemp;
    int endtemp;

    cout << "Geef begin en eind temperatuur in om om te zetten met een bepaalde step";
    cout << "step: ";
    cin >> step ;
    cout << "begintemperatuur in celsius: ";
    cin >> starttemp;
    cout << "eindtemperatuur in celius: ";
    cin >> endtemp;

    int const nlength = (step + endtemp - starttemp) / step;
    vector<int> arrayc( nlength );  // Important: don't use curly braces here! See comment by M.M.

    for( int i = 0;  i < nlength; ++i )
    {
        arrayc[i] = starttemp + i*step;
    }

    cout << nlength << " temperatures in CELSIUS:\n";
    cout << "\n";
    for( int i = 0;  i < nlength; ++i )
    {
        cout << arrayc[i] << "\n";
    }
}

答案 1 :(得分:0)

由于您正在处理可变长度数组,因此需要先使用new[]进行分配,然后才能将数据读入其中。例如:

#include <iostream>
#include <limits>

using namespace std;

void creatingarray(int starttemp, int *arrayc, int nlength, int step)
{
    int newtemp = starttemp;        
    for (int i = 0; i < nlength; ++i)
    {
        arrayc[i] = newtemp;
        newtemp += step;
    }
}

int main()
{
    int step;
    int starttemp;
    int endtemp;

    cout << "Geef begin en eind temperatuur in om om te zetten met een bepaalde step." << endl;

    cout << "step: ";
    cin >> step;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    cout << "begintemperatuur in celsius: ";
    cin >> starttemp;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    cout << "eindtemperatuur in celius: ";
    cin >> endtemp;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    int nlength = (endtemp - starttemp) / step;
    int *arrayc = new int[nlength];

    creatingarray(starttemp, arrayc, nlength, step);

    cout << "Length " << nlength << endl;    

    cout << "CELSIUS" << endl;
    for (int i = 0; i < nlength; ++i)
        cout << arrayc[i] << " ";

    delete[] arrayc;
    return 0;
}

话虽如此,请考虑改为使用std::vector

#include <iostream>
#include <vector>
#include <limits>

using namespace std;

void creatingarray(int starttemp, std::vector<int> &arrayc, int nlength, int step)
{
    int newtemp = starttemp;        
    for (int i = 0; i < nlength; ++i)
    {
        arrayc.push_back(newtemp);
        newtemp += step;
    }
}

int main()
{
    int step;
    int starttemp;
    int endtemp;

    cout << "Geef begin en eind temperatuur in om om te zetten met een bepaalde step." << endl;

    cout << "step: ";
    cin >> step;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    cout << "begintemperatuur in celsius: ";
    cin >> starttemp;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    cout << "eindtemperatuur in celius: ";
    cin >> endtemp;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    int nlength = (endtemp - starttemp) / step;

    std::vector<int> arrayc;
    arrayc.reserve(nlength);

    creatingarray(starttemp, arrayc, nlength, step);

    cout << "Length " << nlength << endl;    

    cout << "CELSIUS" << endl;
    for (int i = 0; i < nlength; ++i)
        cout << arrayc[i] << " ";

    return 0;
}