first, init the pointer (do a sizeof on myTestStruct now and you'll see that it's 4B(or8B on x64) of size), eg teststruct * myTestStruct = new teststruct;
Than, to acess the teststruct to which you have a pointer, you derefrence your pointer as this:
*myTestStruct
which means that from now on this: (*myTestStruct) from your second example will equal myTeststruct from your second.
A structure object can only use the { ......} value list at initialisation.
This is initialization: teststruct myStruct= {0, 1, 1, 0, 60 }; //this is initialization
This is NOT initialization:
1 2 3 4 5
teststruct myStruct;
myStruct = {0, 1, 1, 0, 60 }; //Error - myStruct already exist and has been default initialized -
//this is assignment - and you can't assign to a struct using this method.
//You must set the individual structure member values.
//or you can assign one structure to another
I don't understand what's the point you're trying to convey, but that doesn't compile. You're trying to use the dot operator on a non-object on line 14.
The dot is to indicate the variable "num" in the struct "myTestStruct2".
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <cstdlib>
usingnamespace std;
struct data {
int day;
int month;
int year;
};
int main (void){
data today;
today.day = 28; //dot is indicating "day" in struct "today"
today.month = 8; //month is indicating "day" in struct "today"
today.year = 2009; //year is indicating "day" in struct "today"
cout <<"Today is "<<today.day<<"/"<<today.month<<"/"<<today.year<<endl;
system ("pause");
return EXIT_SUCCESS;
}
"myTestStruct2" Is a pointer of type "teststruct", then he only can point to a address memory of a variable of type "teststruct", and not other type of any differents primary(int, char, etc) or compound variables (structs).
EDIT: i think what i assigned on line 13 is legal, because the variable and the pointer are of same type("teststruct").