2017-02-13 4 views
0

問題は、私のポインタが常に同じメモリアドレスを指していることです。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; 
} 
+3

あなたは 'のstd :: vector'を使用してオフ**ずっと**良いだろう。 –

+0

まず、 'int * arrayc [nlength];'のためにあなたのコードはコンパイルされません。配列サイズを非const値で指定することはできません。 –

+0

@ Zhou - 一部のコンパイラでは、CのVLAを拡張機能として使用できます。 –

答えて

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"; 
    } 
} 
+0

ありがとう、私はこれが正確に私が必要なものを行うが、別の方法で行うことを参照してください:)私は参照によって配列を構築することは本当に可能ではないと思います+ + – Stephane

+0

それは可能です、あなたはそれをやっている方法ではありません。 –

+0

'ベクトル arrayc {nlength};'値がnlengthの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; 
} 
関連する問題