I have address of an struct store in int variable. Can I dereference the struct??
say: int x = 3432234; // memory address of struct now I want to access the some struct from it.
I have been suggest these steps.
But don't know how to apply.
If you have a struct foo
, the easy way is to not use an int x
but make the type a pointer-to-struct foo
, struct foo *x
. One can use the arrow operator to access members from a valid pointer directly.
#include <stdio.h>
struct foo { int a, b; };
int main(void) {
struct foo foo = { 1, 2 };
struct foo *x = &foo;
printf("%d, %d\n", x->a, x->b);
}
Sometimes, hardware requirements complicate this, but this is the simple solution.