A pointer
is a variable that holds the location of, literally the address-of, a variable stored in computer memory. Dereferencing a pointer means getting the value stored in the memory at the address which the pointer “points” to.
So, to access structure individual members using a pointer to that structure, your pointer variable should point to some valid memory address at which memory is allocated to a structure variable.
But ptr
pointer to structure WFC_STRUCT
in your program doesn't point to a valid structure variable of type WFC_STRUCT
in the valid memory address space. So, there is no point to access structure variable using ptr
.
Also, WFC_STRUCT *ptr = (WFC_STRUCT*) NULL
cast is unnessesary.
If you want to access a structure variable using pointer, you can do this in two ways:
WFC_STRUCT *ptr = malloc(sizeof(WFC_STRUCT));
OR
WFC_STRUCT *ptr;
WFC_STRUCT var;
ptr = &var;
ptr->a = 20;
Remenber, if you allocate some memory space using malloc
function call, you need to call free
function to deallocate the memory space when it is not needed anymore. However, in the second method, as variables are created over stack, all the automatic
variable will be destroyed as function returns.