I am required to access some elements from nested structure without using .
and ->
I need to print out the values for keyValue
More straightforward and simpler 3 Line code could be...
//Get the address of the tesla
char *addressoftesla = (laptop_S*)(&tesla);
//structure of tesla = simpleMouse + qwerty + 2 byte paddings
/* Get the value at the starting address of qwerty i.e keyValue*/
printf("keyValue of tesla keyboard is %c\n",*(addressoftesla+sizeof(mouse_S)));
/* Get the value at the starting address of qwerty i.e alternatekeyValue */
printf("alternatekeyValue of tesla keyboard is %c\n",*(addressoftesla+sizeof(mouse_S)+1));
/*
* More info for further understanding:
* A simple check to see all the values in the each field of structure
* tesla - you can also use sizeof to get the each structure and entire
* structure byte sizes and the same can be done using offsetof as in
* other solution
*/
for(int i = 0; i< 12; i++)
{
printf("value at tesla[i] is %d \n",*(addressoftesla+i));
}