Type-casting with Character Pointer
In this section, you are going to learn
How to manipulate integer using character pointer ?
How to manipulate short integer using character pointer ?
How to manipulate structure using character pointer ?
How to swap bytes using character pointer ?
How to manipulate heap using character pointer ?
Topics in this section,
Example 1 : Manipulate Integer : Using character pointer pointing to an integer
Example 2 : Manipulate Short Integer : Using character pointer pointing to a short integer
Example 3 : Manipulate Structure : Using character pointer pointing to a structure
Example 4 : Manipulate Integer Array : Using character pointer pointing to an integer array
Example 5 : Swap two bytes in an Integer using character pointer
Example 6 : Swap two bytes in an Integer using character pointer : Bitwise logic
Example 7 : Swap two bytes in an Integer using character pointer : Bitwise logic
Example 8 : Swap two bytes in a Short Integer using character pointer : Bitwise logic
Example 9 : Swap two bytes in a Short Integer using character pointer : Bitwise logic
Example 10 : Integer pointer arithmetic changes after type conversion to Chracter pointer
Example 11 : Manipulate Heap : Compare heap access with char and int pointer
How to change content of an Integer using character pointer ?
1#include <stdio.h>
2
3int main(void)
4{
5 unsigned int x = 0x11223344;
6
7 unsigned char *ptr;
8
9 // Step 1 : ptr is pointing to a block of 4 bytes
10 ptr = (unsigned char *) &x;
11
12 // Step 2 : ptr can access all 4 bytes for read
13 printf("\nPrint before change : Using character pointer\n");
14 for (int i = 0; i < sizeof(int); i++)
15 {
16 printf("ptr[%d] = %x\n", i, ptr[i]);
17 }
18
19 printf("\nPrint before change : Using integer\n");
20 printf("x = %x\n", x);
21
22 // Step 3 : ptr can access all 4 bytes for write
23 ptr[0] = 0xaa;
24 ptr[1] = 0xbb;
25 ptr[2] = 0xcc;
26 ptr[3] = 0xdd;
27
28 printf("\nPrint after change : Using character pointer\n");
29 for (int i = 0; i < sizeof(int); i++)
30 {
31 printf("ptr[%d] = %x\n", i, ptr[i]);
32 }
33
34 printf("\nPrint after change : Using integer\n");
35 printf("x = %x\n", x);
36
37 return 0;
38}
1Print before change : Using character pointer
2ptr[0] = 44
3ptr[1] = 33
4ptr[2] = 22
5ptr[3] = 11
6
7Print before change : Using integer
8x = 11223344
9
10Print after change : Using character pointer
11ptr[0] = aa
12ptr[1] = bb
13ptr[2] = cc
14ptr[3] = dd
15
16Print after change : Using integer
17x = ddccbbaa
How to change content of a Short Integer using character pointer ?
1#include <stdio.h>
2
3int main(void)
4{
5 unsigned short int x = 0x1122;
6
7 unsigned char *ptr;
8
9 // Step 1 : ptr is pointing to a block of 2 bytes
10 ptr = (unsigned char *) &x;
11
12 // Step 2 : ptr can access all 2 bytes for read
13 printf("\nPrint before change : Using character pointer\n");
14 for (int i = 0; i < sizeof(unsigned short int); i++)
15 {
16 printf("ptr[%d] = %x\n", i, ptr[i]);
17 }
18
19 printf("\nPrint before change : Using integer\n");
20 printf("x = %x\n", x);
21
22 // Step 3 : ptr can access all 2 bytes for write
23 ptr[0] = 0xaa;
24 ptr[1] = 0xbb;
25
26 printf("\nPrint after change : Using character pointer\n");
27 for (int i = 0; i < sizeof(unsigned short int); i++)
28 {
29 printf("ptr[%d] = %x\n", i, ptr[i]);
30 }
31
32 printf("\nPrint after change : Using integer\n");
33 printf("x = %x\n", x);
34
35 return 0;
36}
1Print before change : Using character pointer
2ptr[0] = 22
3ptr[1] = 11
4
5Print before change : Using integer
6x = 1122
7
8Print after change : Using character pointer
9ptr[0] = aa
10ptr[1] = bb
11
12Print after change : Using integer
13x = bbaa
How to change content of a Structure using character 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 char *ptr;
15
16 // Step 1 : ptr is pointing to a block of 12 bytes
17 ptr = (unsigned char *) &x;
18
19 // Step 2 : ptr can access all 12 bytes for read
20 printf("\nPrint before change : Using character pointer\n");
21 for (int i = 0; i < sizeof(struct ABC); i++)
22 {
23 printf("ptr[%d] = %x\n", i, ptr[i]);
24 }
25
26 printf("\nPrint before change : Using Structure\n");
27 printf("x.a = %x\n", x.a);
28 printf("x.b = %x\n", x.b);
29 printf("x.c = %x\n", x.c);
30
31 // Step 3 : ptr can access all 12 bytes for write
32
33 // Step 3.1 : Change x.a using ptr
34 ptr[0] = 0x11;
35 ptr[1] = 0x11;
36 ptr[2] = 0x11;
37 ptr[3] = 0x11;
38
39 // Step 3.2 : Change x.b using ptr
40 ptr[4] = 0x22;
41 ptr[5] = 0x22;
42 ptr[6] = 0x22;
43 ptr[7] = 0x22;
44
45 // Step 3.3 : Change x.c using ptr
46 ptr[8] = 0x33;
47 ptr[9] = 0x33;
48 ptr[10] = 0x33;
49 ptr[11] = 0x33;
50
51 printf("\nPrint after change : Using character pointer\n");
52 for (int i = 0; i < sizeof(struct ABC); i++)
53 {
54 printf("ptr[%d] = %x\n", i, ptr[i]);
55 }
56
57 printf("\nPrint after change : Using Structure\n");
58 printf("x.a = %x\n", x.a);
59 printf("x.b = %x\n", x.b);
60 printf("x.c = %x\n", x.c);
61
62 return 0;
63}
1Print before change : Using character pointer
2ptr[0] = 44
3ptr[1] = 33
4ptr[2] = 22
5ptr[3] = 11
6ptr[4] = 88
7ptr[5] = 77
8ptr[6] = 66
9ptr[7] = 55
10ptr[8] = cc
11ptr[9] = bb
12ptr[10] = aa
13ptr[11] = 99
14
15Print before change : Using Structure
16x.a = 11223344
17x.b = 55667788
18x.c = 99aabbcc
19
20Print after change : Using character pointer
21ptr[0] = 11
22ptr[1] = 11
23ptr[2] = 11
24ptr[3] = 11
25ptr[4] = 22
26ptr[5] = 22
27ptr[6] = 22
28ptr[7] = 22
29ptr[8] = 33
30ptr[9] = 33
31ptr[10] = 33
32ptr[11] = 33
33
34Print after change : Using Structure
35x.a = 11111111
36x.b = 22222222
37x.c = 33333333
How to change content of an Integer array using character pointer ?
1#include <stdio.h>
2
3int main(void)
4{
5 unsigned int x[] = {0x11111111, 0x22222222, 0x33333333, 0x44444444 };
6
7 unsigned char *ptr;
8
9 // Step 1 : ptr is pointing to a block of 16 bytes
10 ptr = (unsigned char *) x;
11
12 // Step 2 : ptr can access all 16 bytes for read
13 printf("\nBefore Change : Print using character pointer\n");
14 for (int i = 0; i < sizeof(x); i++)
15 {
16 printf("ptr[%d] = %x\n", i, ptr[i]);
17 }
18
19 // Step 3 : ptr can access all 16 bytes for write
20 for (int i = 0; i < sizeof(x); i++)
21 {
22 ptr[i] = 0xaa;
23 }
24
25 printf("\nAfter Change : Print using character pointer\n");
26 for (int i = 0; i < sizeof(x); i++)
27 {
28 printf("ptr[%d] = %x\n", i, ptr[i]);
29 }
30
31 printf("\nAfter Change : Print using integer array\n");
32 printf("x[0] = %x\n", x[0]);
33 printf("x[1] = %x\n", x[1]);
34 printf("x[2] = %x\n", x[2]);
35 printf("x[3] = %x\n", x[3]);
36
37 return 0;
38}
1Before Change : Print using character pointer
2ptr[0] = 11
3ptr[1] = 11
4ptr[2] = 11
5ptr[3] = 11
6ptr[4] = 22
7ptr[5] = 22
8ptr[6] = 22
9ptr[7] = 22
10ptr[8] = 33
11ptr[9] = 33
12ptr[10] = 33
13ptr[11] = 33
14ptr[12] = 44
15ptr[13] = 44
16ptr[14] = 44
17ptr[15] = 44
18
19After Change : Print using character pointer
20ptr[0] = aa
21ptr[1] = aa
22ptr[2] = aa
23ptr[3] = aa
24ptr[4] = aa
25ptr[5] = aa
26ptr[6] = aa
27ptr[7] = aa
28ptr[8] = aa
29ptr[9] = aa
30ptr[10] = aa
31ptr[11] = aa
32ptr[12] = aa
33ptr[13] = aa
34ptr[14] = aa
35ptr[15] = aa
36
37After Change : Print using integer array
38x[0] = aaaaaaaa
39x[1] = aaaaaaaa
40x[2] = aaaaaaaa
41x[3] = aaaaaaaa
How to swap two bytes in an Integer using character pointer ?
1/* Program to swap byte 0 and byte 3 of an integer */
2#include <stdio.h>
3
4int main(void)
5{
6 unsigned int x = 0x11223344;
7
8 unsigned char *ptr;
9
10 unsigned char temp;
11
12 // Step 1 : ptr is pointing to a block of 4 bytes
13 ptr = (unsigned char *) &x;
14
15 printf("Before swap : x = %x\n", x);
16
17 temp = ptr[0];
18 ptr[0] = ptr[3];
19 ptr[3] = temp;
20
21 printf("After swap : x = %x\n", x);
22
23 return 0;
24}
1Before swap : x = 11223344
2After swap : x = 44223311
How to swap two bytes in an Integer using character pointer ?
1/* Program to swap byte 0 and byte 3 of an integer */
2#include <stdio.h>
3
4int main(void)
5{
6 unsigned int x = 0x11223344;
7
8 unsigned char *ptr;
9
10 // Step 1 : ptr is pointing to a block of 4 bytes
11 ptr = (unsigned char *) &x;
12
13 printf("Before swap : x = %x\n", x);
14
15 ptr[0] ^= ptr[3];
16 ptr[3] ^= ptr[0];
17 ptr[0] ^= ptr[3];
18
19 printf("After swap : x = %x\n", x);
20
21 return 0;
22}
1Before swap : x = 11223344
2After swap : x = 44223311
How to swap two bytes in an Integer using character pointer ?
1/* Program to swap byte 0 and byte 3 of an integer */
2#include <stdio.h>
3
4int main(void)
5{
6 unsigned int x = 0x11223344;
7
8 printf("Before swap : x = %x\n", x);
9
10 ((unsigned char *) &x)[0] ^= ((unsigned char *) &x)[3];
11 ((unsigned char *) &x)[3] ^= ((unsigned char *) &x)[0];
12 ((unsigned char *) &x)[0] ^= ((unsigned char *) &x)[3];
13
14 printf("After swap : x = %x\n", x);
15
16 return 0;
17}
1Before swap : x = 11223344
2After swap : x = 44223311
How to swap two bytes in a Short Integer using character pointer ?
1/* Program to swap byte 0 and byte 1 of an integer */
2#include <stdio.h>
3
4int main(void)
5{
6 unsigned short int x = 0x1122;
7
8 unsigned char *ptr;
9
10 // Step 1 : ptr is pointing to a block of 2 bytes
11 ptr = (unsigned char *) &x;
12
13 printf("Before swap : x = %x\n", x);
14
15 ptr[0] ^= ptr[1];
16 ptr[1] ^= ptr[0];
17 ptr[0] ^= ptr[1];
18
19 printf("After swap : x = %x\n", x);
20
21 return 0;
22}
1Before swap : x = 1122
2After swap : x = 2211
How to swap two bytes in a Short Integer using character pointer ?
1/* Program to swap byte 0 and byte 1 of an integer */
2#include <stdio.h>
3
4int main(void)
5{
6 unsigned short int x = 0x1122;
7
8 printf("Before swap : x = %x\n", x);
9
10 ((unsigned char *) &x)[0] ^= ((unsigned char *) &x)[1];
11 ((unsigned char *) &x)[1] ^= ((unsigned char *) &x)[0];
12 ((unsigned char *) &x)[0] ^= ((unsigned char *) &x)[1];
13
14 printf("After swap : x = %x\n", x);
15
16 return 0;
17}
1Before swap : x = 1122
2After swap : x = 2211
Observe that Integer Pointer increments by 4 Bytes
1#include <stdio.h>
2
3int main(void)
4{
5 unsigned int x[] = { 0x11223344, 0x55667788, 0x99aabbcc };
6
7 unsigned int *p;
8
9 p = x; // p is pointing to x[0]
10
11 p++; // p is pointing to x[1]
12
13 printf("*p = %x\n", *p);
14
15 return 0;
16}
1*p = 55667788
Observe that Integer Pointer is typecasted to Char Pointer and hence increments by 1 Byte
1#include <stdio.h>
2
3int main(void)
4{
5 unsigned int x[] = { 0x11223344, 0x55667788, 0x99aabbcc };
6
7 unsigned int *p;
8
9 unsigned char *cp;
10
11 p = x; // p is pointing to x[0]
12
13 cp = (unsigned char *)p + 1; // cp is pointing second byte of x[0]
14
15 printf("*cp = %x\n", *cp);
16
17 return 0;
18}
1*cp = 33
Both pointers ip and cp are pointing to a single block of 40 bytes in heap
ip can index from 0 to 9
cp can index from 0 to 39
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 unsigned int *ip;
7
8 unsigned char *cp;
9
10 // Allocate heap memory of 40 bytes
11 // ip views this as 10 integers
12 ip = malloc(40);
13
14 // cp is also pointing to memory allocated in previous line
15 // cp views this as 40 characters
16 cp = (unsigned char *) ip;
17
18 // Change bytes 36, 37, 38, 39 using ip
19 ip[9] = 0x11223344;
20 printf("Changed using ip : ip[9] = %x\n", ip[9]);
21
22 // Change bytes 36, 37, 38, 39 using cp
23 cp[36] = 0xaa;
24 cp[37] = 0xbb;
25 cp[38] = 0xcc;
26 cp[39] = 0xdd;
27 printf("Changed using cp : ip[9] = %x\n", ip[9]);
28
29 return 0;
30}
1Changed using ip : ip[9] = 11223344
2Changed using cp : ip[9] = ddccbbaa
Learning |
Description |
---|---|
ptr = (unsigned char *) &x; |
|
ptr = (unsigned char *) x; |
|
((unsigned char *) &x)[3]; |
|
cp = (unsigned char *)p + 1; |
|
Current Module
Previous Module
Next Module
Other Modules