I got a struct and an initializing function like following:
struct someStruct {
int* b, r; // r is a pointer to an integer in b
};
someStruct* init_struct(size_t size) {
someStruct* p = (someStruct*) malloc(sizeof(someStruct));
p->b = (int*) malloc(size);
p->b[0] = 16;
p->r = &p->b; // Here's the error, why?
return p;
}
In your code the
rfield is an integer, not a pointer to an integer ! Also, &(p->b) is a pointer to a pointer to an integer (int**). You need to define your struct like this if you want r to be pointing a integer in the b array :And assign the r pointer by dereferencing an element of b (
int), not the pointer itself (int*):Or don’t dereference b at all if you want to point to
b[0]: