In C, what happens in memory when we do cast int to struct*? -
typedef struct block { size_t size; struct block* next; } node; static char arr[1000];
what happens arr
when do
node* first_block = (node*)arr;
?
i understand equal to
node* first_block = (node*)&arr[0];
but
sizeof(node) = 8; sizeof(arr[0])= 1;
so first element override next 7 elements in arr, because it's struct ? can explain me cast, please ?
when write
node* first_block = (node*)arr;
you not changing in memory, pointer area in memory, type of pointer determines how area treated in regard pointer arithmetic.
first_block->next
pointer determined characters in array.
as comparison have char* pointer same array
(if arr declared @ global scope contain 0's)
char* q = arr; node* p = (node*)arr; arr[1000] +----------------------+ q -> | | | | | | 0 | 0 | ... | 0 | p -> | | | | | +----------------------+
when do
q = q + 1; // move character pointer 1 char forward since q char pointer
when do
p = p + 1; // move node pointer sizeof(node) chars forward since p node pointer
when *q character value of q points to, *p gives node value char arr, i.e. characters interpreted node struct.
Comments
Post a Comment