Typecasting exercise ========================= .. tab-set:: .. tab-item:: Question 1 : What is the output of below code and why ? .. code-block:: c :linenos: #include int main(void) { char arr[] = { 0x11, 0x22, 0x33, 0x44 }; unsigned int *ptr; ptr = (unsigned int *)arr; printf("*ptr = %x\n", *ptr); return 0; } .. dropdown:: See Answer .. code-block:: c :linenos: *ptr = 44332211 * Note that memory is organised as below in array and integer * 0x11 is byte 0 * 0x22 is byte 1 * 0x33 is byte 2 * 0x44 is byte 3 * printf prints bytes from right to left .. tab-set:: .. tab-item:: Question 2 : What is the output of below code and why ? .. code-block:: c :linenos: #include int main(void) { char arr[] = { 0x44, 0x33, 0x22, 0x11 }; unsigned int *ptr; ptr = (unsigned int *)arr; printf("*ptr = %x\n", *ptr); return 0; } .. dropdown:: See Answer .. code-block:: c :linenos: *ptr = 11223344 * Note that memory is organised as below in array and integer * 0x44 is byte 0 * 0x33 is byte 1 * 0x22 is byte 2 * 0x11 is byte 3 * printf prints bytes from right to left .. tab-set:: .. tab-item:: Question 3 : What is the output of below code and why ? .. code-block:: c :linenos: #include int main(void) { unsigned int a = 0x11223344; unsigned char *ptr; ptr = (unsigned char *)&a; printf("ptr[0] = %x\n", ptr[0]); printf("ptr[1] = %x\n", ptr[1]); printf("ptr[2] = %x\n", ptr[2]); printf("ptr[3] = %x\n", ptr[3]); return 0; } .. dropdown:: See Answer .. code-block:: c :linenos: ptr[0] = 44 ptr[1] = 33 ptr[2] = 22 ptr[3] = 11 * Note that memory is organised as below in integer * 0x44 is byte 0 * 0x33 is byte 1 * 0x22 is byte 2 * 0x11 is byte 3 * printf prints bytes from right to left .. tab-set:: .. tab-item:: Question 4 : What is the output of below code and why ? .. code-block:: c :linenos: #include int main(void) { unsigned int a = 0x44332211; unsigned char *ptr; ptr = (unsigned char *)&a; printf("ptr[0] = %x\n", ptr[0]); printf("ptr[1] = %x\n", ptr[1]); printf("ptr[2] = %x\n", ptr[2]); printf("ptr[3] = %x\n", ptr[3]); return 0; } .. dropdown:: See Answer .. code-block:: c :linenos: ptr[0] = 11 ptr[1] = 22 ptr[2] = 33 ptr[3] = 44 * Note that memory is organised as below in integer * 0x11 is byte 0 * 0x22 is byte 1 * 0x33 is byte 2 * 0x44 is byte 3 * printf prints bytes from right to left .. tab-set:: .. tab-item:: Question 5 : What is the output of below code and why ? .. code-block:: c :linenos: #include unsigned char buffer[] = {0x11, 0x22, 0x45, 0x67, 0xc0, 0xb5, 0xd7, 0x0b, 0xac, 0x5b}; struct hdr { unsigned short int fc; unsigned short int duration; unsigned char ra_addr[6]; }; int main(void) { struct hdr *phdr; phdr = (struct hdr *) buffer; printf("phdr->fc = %x\n", phdr->fc); printf("phdr->duration = %x\n", phdr->duration); printf("phdr->ra_addr[0] = %x\n", phdr->ra_addr[0]); printf("phdr->ra_addr[1] = %x\n", phdr->ra_addr[1]); printf("phdr->ra_addr[2] = %x\n", phdr->ra_addr[2]); printf("phdr->ra_addr[3] = %x\n", phdr->ra_addr[3]); printf("phdr->ra_addr[4] = %x\n", phdr->ra_addr[4]); printf("phdr->ra_addr[5] = %x\n", phdr->ra_addr[5]); return 0; } .. dropdown:: See Answer .. code-block:: c :linenos: phdr->fc = 2211 phdr->duration = 6745 phdr->ra_addr[0] = c0 phdr->ra_addr[1] = b5 phdr->ra_addr[2] = d7 phdr->ra_addr[3] = b phdr->ra_addr[4] = ac phdr->ra_addr[5] = 5b .. card:: See Also * Current Module * :doc:`typecasting_n_ptr` * Previous Module * :doc:`../malloc_ptr/malloc_ptr` * Next Module * :doc:`../funcs_n_ptrs/funcs_n_ptrs` * Other Modules * :doc:`../variable_and_ptr/variable_and_ptr` * :doc:`../array_n_ptrs/array_n_ptrs` * :doc:`../memcpy_ptr/memcpy_ptr` * :doc:`../const_ptr/const_ptr` * :doc:`../void_ptr/void_ptr` * :doc:`../array_of_ptr/array_of_ptr` * :doc:`../ptr_to_array/ptr_to_array` * :doc:`../function_ptr/function_ptr` * :doc:`../pre_incr_ptr/pre_incr_ptr` * :doc:`../post_incr_ptr/post_incr_ptr` * :doc:`../pre_decr_ptr/pre_decr_ptr` * :doc:`../post_decr_ptr/post_decr_ptr`