Example code:
struct MyStruct {
char c;
int i;
double d;
char* get_c() {
return &c;
}
int* get_i() {
return &i;
}
double* get_d() {
return &d;
}
};
void foo() {
MyStruct s;
char* pc = s.get_c();
*pc = 'a';
*(s.get_i()) = 1;
double& rd = *(s.get_d());
rd = 3.14;
}
This code uses a local pointer variable, a temporary, and a local reference to access members of the struct. All of these should work.
Note my use of accessor functions: you didn't state what you intended, but if you're accessing these member variables multiple times, it's usually a good idea to have a function for that purpose.
P.S.:
Member 10964099 wrote:
"Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion"
This error pretty clearly states what you did wrong: the right hand side obviously evaluates to an integer value, but the left hand side evaluates to a type that the compiler can't assign an integer to. The reason is that your pointer variable is not of the appropriate type - it must be pointer to
int
, or pointer to any other type that an int can be converted into, e. g. pointer to
long
or pointer to
double
.