-2
struct型のデータ入力からfunction aging()で年齢を1増やしたいと思います。ポインタ付きのC++構造体配列
私は構造体の配列としてデータ入力を取得し、ポインタ型として構造体引数を取得したいが、唯一の失敗する(年齢が増加していない)
問題は何ですか?
#include <iostream>
using namespace std;
struct Person
{
int age;
double weight, height;
};
void aging(Person* p);
int main()
{
Person ps[2];
for (int i=0; i<2; i++){
cout <<"age :";
cin >> ps[i].age;
cout <<"weight :";
cin >> ps[i].weight;
cout <<"height :";
cin >> ps[i].height;
}
aging(&ps[2]);
for (int i=0; i<2; i++){
cout <<"age after1: "<<ps[i].age<<"weight after1: "<<ps[i].weight<<"height after1:"<<ps[i].height<<"\n";
}
return 0;
}
void aging(Person* p){
p-> age++;
}
「問題は何ですか?」これは私が知りたいことです!それはコンパイラエラーですか(もしそうなら、あなたはなぜそれを含んでいませんか?)それは実行時ですか? (もしそうなら、あなたはそれをなぜ記述しなかったのでしょうか?)... – John3136
年齢は増加しません –
これをValgrindの下で実行した場合、すぐにあなたは '&ps [ 2]。代わりに '&ps [1]'エージングを考慮してください。 –