Type-casting with Integer Pointer ==================================== * In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to manipulate structure using integer pointer ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Behaviour of structure pointer arithmetic ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Example 1 : integer pointer pointing to a structure ` * :ref:`Example 2 : Structure pointer arithmetic changes after type conversion to Integer pointer ` * :ref:`Example 3 : Compare heap access with char, int and struct pointer ` * :ref:`Summary ` .. _typecasting_n_ptr_sp_int_ptr_ex_1: .. tab-set:: .. tab-item:: Example 1 : integer pointer pointing to a structure .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to change content of a Structure using integer pointer ? .. code-block:: c :linenos: :emphasize-lines: 12, 14, 17 #include struct ABC { int a; int b; int c; }; int main(void) { struct ABC x = { .a = 0x11223344, .b = 0x55667788, .c = 0x99aabbcc }; unsigned int *ptr; // Step 1 : ptr is pointing to a block of 12 bytes ptr = (unsigned int *) &x; printf("\nPrint structure before change\n"); printf("x.a = %x\n", x.a); printf("x.b = %x\n", x.b); printf("x.c = %x\n", x.c); // Step 3.1 : ptr can access all 12 bytes for write // Step 3.2 : ptr can access only 0, 1, 2 indexes //this changes x.a ptr[0] = 0xaaaaaaaa; //this changes x.b ptr[1] = 0xbbbbbbbb; //this changes x.c ptr[2] = 0xcccccccc; printf("\nPrint structure after change\n"); printf("x.a = %x\n", x.a); printf("x.b = %x\n", x.b); printf("x.c = %x\n", x.c); return 0; } .. tab-set:: .. tab-item:: Output .. code-block:: c :linenos: :emphasize-lines: 1, 6 Print structure before change x.a = 11223344 x.b = 55667788 x.c = 99aabbcc Print structure after change x.a = aaaaaaaa x.b = bbbbbbbb x.c = cccccccc .. _typecasting_n_ptr_sp_int_ptr_ex_2: .. tab-set:: .. tab-item:: Example 2 : Structure pointer arithmetic changes after type conversion to Integer pointer .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Observe that Structure Pointer increments by size of structure Bytes .. code-block:: c :linenos: :emphasize-lines: 9, 11 #include struct ABC { int a; int b; int c; int d; }; int main(void) { struct ABC x[] = { {1, 2, 3, 4 }, {5, 6, 7, 8 }, {9, 10, 11, 12 }, }; struct ABC *p; p = x; // p is pointing to x[0] p++; // p is pointing to x[1] printf("p->a = %x\n", p->a); //prints 5 return 0; } .. tab-set:: .. tab-item:: Output .. code-block:: c :linenos: p->a = 5 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Observe that Structure Pointer is typecasted to Integer Pointer and hence increments by 4 Bytes .. code-block:: c :linenos: :emphasize-lines: 24 #include struct ABC { int a; int b; int c; int d; }; int main(void) { struct ABC x[] = { {1, 2, 3, 4 }, {5, 6, 7, 8 }, {9, 10, 11, 12 }, }; struct ABC *p; unsigned int *ip; p = x; // p is pointing to x[0] ip = (unsigned int *)p + 1; // ip is pointing second byte of x[0].b printf("ip = %x\n", *ip); //prints 2 return 0; } .. tab-set:: .. tab-item:: Output .. code-block:: c :linenos: ip = 2 .. _typecasting_n_ptr_sp_int_ptr_ex_3: .. tab-set:: .. tab-item:: Example 3 : Compare heap access with char, int and struct pointer .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * 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 .. code-block:: c :linenos: :emphasize-lines: 12, 14, 16, 20, 26, 30 #include #include struct ABC { int a; int b; int c; }; int main(void) { unsigned char *cp; unsigned int *ip; struct ABC *sp; // Allocate heap memory of 36 bytes // sp views this as 3 structure blocks, where each block is 12 Bytes sp = malloc(36); printf("sizeof(struct ABC) = %d\n", (int) sizeof(struct ABC)); // cp is also pointing to memory allocated in previous line // cp views this as 36 chracaters, where each block is 1 Byte cp = (unsigned char *) sp; // ip is also pointing to memory allocated in previous line // ip views this as 9 integers, where each block is 4 Bytes ip = (unsigned int *) sp; // Change bytes 32, 33, 34, 35 using cp cp[32] = 0x11; cp[33] = 0x22; cp[34] = 0x33; cp[35] = 0x44; printf("Changed using cp : sp[2].c = %x\n", sp[2].c); // Change bytes 32, 33, 34, 35 using ip ip[8] = 0x33333333; printf("Changed using ip : sp[2].c = %x\n", sp[2].c); // Change bytes 32, 33, 34, 35 using sp sp[2].c = 0x44445555; printf("Changed using sp : sp[2].c = %x\n", sp[2].c); return 0; } .. tab-set:: .. tab-item:: Output .. code-block:: c :linenos: sizeof(struct ABC) = 12 Changed using cp : sp[2].c = 44332211 Changed using ip : sp[2].c = 33333333 Changed using sp : sp[2].c = 44445555 .. _typecasting_n_ptr_sp_int_ptr_ex_4: .. tab-set:: .. tab-item:: Summary ============================= ============================================================================== 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) ============================= ============================================================================== .. 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`