Hey guys , I was just wondering if I wanted to create a dynamic struct like here :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include<iostream>
#include<iomanip>
using namespace std;
struct candy{
char x[20];
double y;
int z;
};
int main(){
candy *ptr = new candy[3];
cin.getline(ptr[0].x, 20);
cin >> ptr[1].y;
cin >> ptr[2].z;
cout << ptr[0].x << endl;
cout << ptr[1].y << endl;
cout << ptr[2].z << endl;
delete ptr;
getchar();
getchar();
return 0;
}
|
this is a dynamic struct , this here:
candy *ptr = new candy[3];
should create 3 structs for type candy:
my question here:
1- this creates 3 struct that goes to the heap memory right ?
2- if I used:
candy *ptr[2] = new candy;
this would create two pointers that points to a type candy , right?
3- what I learned is that when you use a dynamic struct , you should access it's variables by using "->" operator , but when I tried to it gives me an error:
type 'candy' does not have an overloaded member 'operator ->'
why ? I know that there's another method to access it's variables but why won't this work ?