構造体の2D配列を作成し、値を出力しようとしています。どのように「セグメント障害(コアダンプ)」メッセージ」。
#include <iostream>
#include <string>
using namespace std;
struct student{
string name;
int age;
float marks;
};
student* initiateStudent(string name, int age, float marks){
student *studFun;
studFun->name = name;
studFun->age = age;
studFun->marks = marks;
return studFun;
}
int main() {
int totalStudents = 1;
string name;
int age;
float marks;
cin >> totalStudents;
student** stud = new student*[totalStudents];
for(int i=0;i<totalStudents;i++){
stud[i] = new student[1];
cin >> name >> age >> marks;
stud[i] = initiateStudent(name,age,marks);
}
delete [] stud;
return 0;
}
Netbeans for C ++を使用してコンパイルしています。誰かがこのコードの何が問題なのか教えてもらえますか?
これはうまくいくはずです
#include <iostream>
#include <string>
using namespace std;
struct student{
string name;
int age;
float marks;
};
student* initiateStudent(string name, int age, float marks){
student *studFun = new student();
studFun->name = name;
studFun->age = age;
studFun->marks = marks;
return studFun;
}
int main() {
int totalStudents = 1;
string name;
int age;
float marks;
cin >> totalStudents;
student** stud = new student*[totalStudents];
for(int i=0;i<totalStudents;i++){
stud[i] = new student[1];
cin >> name;
cin >> age;
cin >> marks;
stud[i] = initiateStudent(name,age,marks);
}
delete [] stud;
return 0;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加