Type-casting with Structure Pointer

  • In this section, you are going to learn

How to extract formatted data from structure pointer pointing to character array ?

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

 1#include <stdio.h>
 2
 3// Step 1 : Define the desired structure
 4struct ABC {
 5        unsigned int a;
 6        unsigned int b;
 7        unsigned int c;
 8        unsigned int d;
 9};
10
11char data[16] = { 0xfe, 0x12, 0x45, 0x67,
12                  0x92, 0x90, 0x23, 0x11,
13                  0x22, 0x83, 0x81, 0xcc,
14                  0x37, 0xbe, 0xff, 0xad
15                };
16
17int main(void)
18{
19        // Step 2 : Define a structure pointer
20        struct ABC *sp;
21
22
23        // Step 3 : Point structure pointer to start of char array
24        sp = (struct ABC *) data;
25
26        // Step 4 : Now access data using structure members
27        printf("sp->a = %x\n", sp->a);
28        printf("sp->b = %x\n", sp->b);
29        printf("sp->c = %x\n", sp->c);
30        printf("sp->d = %x\n", sp->d);
31
32        return 0;
33}
1sp->a = 674512fe
2sp->b = 11239092
3sp->c = cc818322
4sp->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

1f1 = sp->a & 0xf;
2
3f2 = (sp->a >> 4);
  • Instead of performing bitwise operations we could redefine the structure with bitfields

 1struct ABC {
 2        unsigned int f1 : 4;
 3        unsigned int f2 : 28;
 4        unsigned int b;
 5        unsigned int c;
 6        unsigned int d;
 7};
 8
 9char data[16] = { 0xfe, 0x12, 0x45, 0x67,
10                  0x92, 0x90, 0x23, 0x11,
11                  0x22, 0x83, 0x81, 0xcc,
12                  0x37, 0xbe, 0xff, 0xad
13                };
14
15int main(void)
16{
17        struct ABC *sp;
18
19        sp = (struct ABC *) data;
20
21        printf("sp->f1 = %x\n", sp->f1);
22        printf("sp->f2  = %x\n", sp->f2);
23        printf("sp->b = %x\n", sp->b);
24        printf("sp->c = %x\n", sp->c);
25        printf("sp->d = %x\n", sp->d);
26
27        return 0;
28}
  • Now, what if we want to extract bits 21 to 27 (inclusive of 21 and 27) ?

1f3 = (sp->a & 0x0fd00000) >> 21; // is equal to 0x3a
  • Instead of performing bitwise operations we could redefine the structure with bitfields again !

 1#include <stdio.h>
 2
 3struct ABC {
 4        // bits 0 to 20 inclusive
 5        // Total number of bits in 0..20 is 21
 6        unsigned int f1 : 21;
 7
 8        // bits 21 to 27 inclusive
 9        // Total number of bits in 21..27 is 7
10        unsigned int f2 : 7;
11
12        // bits 28 to 31 inclusive
13        // Total number of bits in 28..31 is 4
14        unsigned int f3 : 4;
15        unsigned int b;
16        unsigned int c;
17        unsigned int d;
18};
19
20char data[16] = { 0xfe, 0x12, 0x45, 0x67,
21                  0x92, 0x90, 0x23, 0x11,
22                  0x22, 0x83, 0x81, 0xcc,
23                  0x37, 0xbe, 0xff, 0xad
24                };
25
26int main(void)
27{
28        struct ABC *sp;
29
30        sp = (struct ABC *) data;
31
32        printf("sp->f1 = %x\n", sp->f1);
33        printf("sp->f2 = %x\n", sp->f2);
34        printf("sp->f3 = %x\n", sp->f3);
35        printf("sp->b  = %x\n", sp->b);
36        printf("sp->c  = %x\n", sp->c);
37        printf("sp->d  = %x\n", sp->d);
38
39        return 0;
40}
1sp->f1 = 512fe
2sp->f2 = 3a
3sp->f3 = 6
4sp->b  = 11239092
5sp->c  = cc818322
6sp->d  = adffbe37

Learning

Description