Answer the question
In order to leave comments, you need to log in
How to bind struct field values to a pointer?
struct Vector {
void *x;
void *y;
};
struct Complex {
int real;
int imagine;
};
int main(){
struct Vector v;
//to do
return 0;
}
v.x.real =5;
v.x.imagine = 7;
Answer the question
In order to leave comments, you need to log in
Exactly the way you want won't work, a null pointer can't have a field. We'll first have to cast it to a pointer to a structure that has the field real
. And since it's a pointer, you need to use the arrow operator to access the field.
struct Vector {
void *x;
void *y;
};
struct Complex {
int real;
int imagine;
};
int main() {
struct Vector v;
struct Complex c1;
v.x = &c1;
((struct Complex*) v.x)->real = 42;
return 0;
}
void * is a pointer to who knows what.
struct Vector {
struct Complex x;
struct Complex y;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question