Type-casting with Structure Pointer ==================================== * In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to extract formatted data from structure pointer pointing to character array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Extract formatted data from character array using Structure Pointer ` * :ref:`Summary ` .. _typecasting_n_ptr_sp_struct_ptr_ex_1: .. tab-set:: .. tab-item:: Extract formatted data from character array using Structure Pointer .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to extract formatted data from raw hex dump stored in char array ? * Step 1 : Define the desired structure * Step 2 : Define a structure pointer * Step 3 : Point structure pointer to start of char array * Step 4 : Now access data using structure members .. code-block:: c :linenos: :emphasize-lines: 20 #include // Step 1 : Define the desired structure struct ABC { unsigned int a; unsigned int b; unsigned int c; unsigned int d; }; char data[16] = { 0xfe, 0x12, 0x45, 0x67, 0x92, 0x90, 0x23, 0x11, 0x22, 0x83, 0x81, 0xcc, 0x37, 0xbe, 0xff, 0xad }; int main(void) { // Step 2 : Define a structure pointer struct ABC *sp; // Step 3 : Point structure pointer to start of char array sp = (struct ABC *) data; // Step 4 : Now access data using structure members printf("sp->a = %x\n", sp->a); printf("sp->b = %x\n", sp->b); printf("sp->c = %x\n", sp->c); printf("sp->d = %x\n", sp->d); return 0; } .. tab-set:: .. tab-item:: Output .. code-block:: c :linenos: sp->a = 674512fe sp->b = 11239092 sp->c = cc818322 sp->d = adffbe37 * Now, let us suppose we want to split ``a`` into two fields * First field is of 4 bits with value of ``0xe`` * Second field is of 28 bits with value of ``0x674512f`` * We can extract using bitwise operations like below .. code-block:: c :linenos: f1 = sp->a & 0xf; f2 = (sp->a >> 4); * Instead of performing bitwise operations we could redefine the structure with bitfields .. code-block:: c :linenos: struct ABC { unsigned int f1 : 4; unsigned int f2 : 28; unsigned int b; unsigned int c; unsigned int d; }; char data[16] = { 0xfe, 0x12, 0x45, 0x67, 0x92, 0x90, 0x23, 0x11, 0x22, 0x83, 0x81, 0xcc, 0x37, 0xbe, 0xff, 0xad }; int main(void) { struct ABC *sp; sp = (struct ABC *) data; printf("sp->f1 = %x\n", sp->f1); printf("sp->f2 = %x\n", sp->f2); printf("sp->b = %x\n", sp->b); printf("sp->c = %x\n", sp->c); printf("sp->d = %x\n", sp->d); return 0; } * Now, what if we want to extract bits 21 to 27 (inclusive of 21 and 27) ? .. code-block:: c :linenos: f3 = (sp->a & 0x0fd00000) >> 21; // is equal to 0x3a * Instead of performing bitwise operations we could redefine the structure with bitfields again ! .. code-block:: c :linenos: #include struct ABC { // bits 0 to 20 inclusive // Total number of bits in 0..20 is 21 unsigned int f1 : 21; // bits 21 to 27 inclusive // Total number of bits in 21..27 is 7 unsigned int f2 : 7; // bits 28 to 31 inclusive // Total number of bits in 28..31 is 4 unsigned int f3 : 4; unsigned int b; unsigned int c; unsigned int d; }; char data[16] = { 0xfe, 0x12, 0x45, 0x67, 0x92, 0x90, 0x23, 0x11, 0x22, 0x83, 0x81, 0xcc, 0x37, 0xbe, 0xff, 0xad }; int main(void) { struct ABC *sp; sp = (struct ABC *) data; printf("sp->f1 = %x\n", sp->f1); printf("sp->f2 = %x\n", sp->f2); printf("sp->f3 = %x\n", sp->f3); printf("sp->b = %x\n", sp->b); printf("sp->c = %x\n", sp->c); printf("sp->d = %x\n", sp->d); return 0; } .. code-block:: c :linenos: sp->f1 = 512fe sp->f2 = 3a sp->f3 = 6 sp->b = 11239092 sp->c = cc818322 sp->d = adffbe37 .. _typecasting_n_ptr_sp_struct_ptr_ex_2: .. tab-set:: .. tab-item:: Summary ============================= ============================================================================== Learning Description ============================= ============================================================================== ============================= ============================================================================== .. 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`