Type-casting with Integer Pointer

  • In this section, you are going to learn

How to manipulate structure using integer pointer ?

Behaviour of structure pointer arithmetic ?

How to change content of a Structure using integer pointer ?

 1#include <stdio.h>
 2
 3struct ABC
 4{
 5        int a;
 6        int b;
 7        int c;
 8};
 9
10int main(void)
11{
12        struct ABC x = { .a = 0x11223344, .b = 0x55667788, .c = 0x99aabbcc };
13
14        unsigned int *ptr;
15
16        // Step 1 : ptr is pointing to a block of 12 bytes
17        ptr = (unsigned int *) &x;
18
19        printf("\nPrint structure before change\n");
20        printf("x.a = %x\n", x.a);
21        printf("x.b = %x\n", x.b);
22        printf("x.c = %x\n", x.c);
23
24        // Step 3.1 : ptr can access all 12 bytes for write
25        // Step 3.2 : ptr can access only 0, 1, 2 indexes
26
27        //this changes x.a
28        ptr[0] = 0xaaaaaaaa;
29
30        //this changes x.b
31        ptr[1] = 0xbbbbbbbb;
32
33        //this changes x.c
34        ptr[2] = 0xcccccccc;
35
36        printf("\nPrint structure after change\n");
37        printf("x.a = %x\n", x.a);
38        printf("x.b = %x\n", x.b);
39        printf("x.c = %x\n", x.c);
40
41        return 0;
42}
1Print structure before change
2x.a = 11223344
3x.b = 55667788
4x.c = 99aabbcc
5
6Print structure after change
7x.a = aaaaaaaa
8x.b = bbbbbbbb
9x.c = cccccccc

Observe that Structure Pointer increments by size of structure Bytes

 1#include <stdio.h>
 2
 3struct ABC {
 4        int a;
 5        int b;
 6        int c;
 7        int d;
 8};
 9
10int main(void)
11{
12        struct ABC x[] = {
13                                {1, 2, 3, 4 },
14                                {5, 6, 7, 8 },
15                                {9, 10, 11, 12 },
16                        };
17
18        struct ABC *p;
19
20        p = x; // p is pointing to x[0]
21
22        p++;   // p is pointing to x[1]
23
24        printf("p->a = %x\n", p->a); //prints 5
25
26        return 0;
27}
1p->a = 5

Observe that Structure Pointer is typecasted to Integer Pointer and hence increments by 4 Bytes

 1#include <stdio.h>
 2
 3struct ABC {
 4        int a;
 5        int b;
 6        int c;
 7        int d;
 8};
 9
10int main(void)
11{
12        struct ABC x[] = {
13                {1, 2, 3, 4 },
14                {5, 6, 7, 8 },
15                {9, 10, 11, 12 },
16        };
17
18struct ABC *p;
19
20unsigned int *ip;
21
22p = x; // p is pointing to x[0]
23
24ip = (unsigned int *)p + 1;   // ip is pointing second byte of x[0].b
25
26printf("ip = %x\n", *ip); //prints 2
27
28return 0;
29}
1ip = 2
  • Three pointers cp, ip and sp are pointing to a single block of 36 bytes in heap

  • cp can index from 0 to 35

  • ip can index from 0 to 8

  • sp can index from 0 to 2

 1#include <stdio.h>
 2#include <stdlib.h>
 3
 4struct ABC {
 5        int a;
 6        int b;
 7        int c;
 8};
 9
10int main(void)
11{
12        unsigned char *cp;
13
14        unsigned int *ip;
15
16        struct ABC *sp;
17
18        // Allocate heap memory of 36 bytes
19        // sp views this as 3 structure blocks, where each block is 12 Bytes
20        sp = malloc(36);
21
22        printf("sizeof(struct ABC) = %d\n", (int) sizeof(struct ABC));
23
24        // cp is also pointing to memory allocated in previous line
25        // cp views this as 36 chracaters, where each block is 1 Byte
26        cp = (unsigned char *) sp;
27
28        // ip is also pointing to memory allocated in previous line
29        // ip views this as 9 integers, where each block is 4 Bytes
30        ip = (unsigned int *) sp;
31
32        // Change bytes 32, 33, 34, 35 using cp
33        cp[32] = 0x11;
34        cp[33] = 0x22;
35        cp[34] = 0x33;
36        cp[35] = 0x44;
37        printf("Changed using cp : sp[2].c = %x\n", sp[2].c);
38
39        // Change bytes 32, 33, 34, 35 using ip
40        ip[8] = 0x33333333;
41        printf("Changed using ip : sp[2].c = %x\n", sp[2].c);
42
43        // Change bytes 32, 33, 34, 35 using sp
44        sp[2].c = 0x44445555;
45        printf("Changed using sp : sp[2].c = %x\n", sp[2].c);
46
47        return 0;
48}
1sizeof(struct ABC) = 12
2Changed using cp : sp[2].c = 44332211
3Changed using ip : sp[2].c = 33333333
4Changed using sp : sp[2].c = 44445555

Learning

Description

ptr = (unsigned int *) &x;

  • Use ptr to manipulate variable x

  • Where x can be integer

  • Where x can be structure

  • Where x can be union

ptr = (unsigned int *) x;

  • Use ptr to manipulate array x

  • Where x can be char array (of size 4 bytes atleast)

  • Where x can be short integer array (of size 4 bytes atleast)

  • Where x can be integer array (of size 4 bytes atleast)

  • Where x can be structure array (of size 4 bytes atleast)

  • Where x can be union array (of size 4 bytes atleast)

((unsigned int *) &x)[3];

  • Access integer 3 of variable x

  • Where x can be structure (of size 16 bytes atleast)

  • Where x can be union (of size 16 Bytes atleast)

cp = (unsigned int *)p + 1;

  • Access integer 1 of memory pointed by p

  • Where p can be an array of any type (pointing to memory of atleast 8 Bytes)

  • Where p can be a pointer of any type (pointing to memory of atleast 8 Bytes)