問題は、私のポインタが常に同じメモリアドレスを指していることです。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;
}
あなたは 'のstd :: vector'を使用してオフ**ずっと**良いだろう。 –
まず、 'int * arrayc [nlength];'のためにあなたのコードはコンパイルされません。配列サイズを非const値で指定することはできません。 –
@ Zhou - 一部のコンパイラでは、CのVLAを拡張機能として使用できます。 –