I'm a C++ noob and now just learning how to dynamically allocate. I am learing by myself mostly googling. As I almost mastered C, I basically know how to dynamically allocate double pointer structure. But I think in C++, the method used in C when dynamically allocating double structure pointer is not right. Here'a the code including main;
=============================================================================================
#include <iostream>
using namespace std;
typedef struct s2 {
int data3;
int data4;
}S2;
typedef struct s1 {
int data;
int data2;
S2* ps2;
}S1;
int main()
{
S1* pt1 = new S1;
S2** pt2 = new S2*;
cout << "data >> "; cin >> pt1->data;
cout << "data2 >> "; cin >> pt1->data2;
cout << "data3 >> "; cin >> pt1->ps2->data3;
cout << "data4 >> "; cin >> pt1->ps2->data4;
cout << pt1->data << " \n " << pt1->data2 << " \n " << pt1->ps2->data3 << " \n " << pt1->ps2->data4 << endl;
return 0;
}
As y'all master coders may already, know when I build this solution, after I inserted integer into pt1->data2, the program suddenly stops and just terminate, not even giving me a chance to insert into variable pt1->ps2->data3 nor pt1->ps2->data4. Are there any solution to this problem? As you can see, my english is very bad because I'm korean and just 19 years old. So could you master coders please write down the entire solution code, and then tell me what is the problem? Thank you very much in advance.