Malloc structures single pointer ================================= * In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to allocate memory using malloc ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to allocate memory using calloc ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to allocate memory using realloc ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Allocate 1 Structure Object using malloc ` * :ref:`Allocate 1 Structure Object using calloc ` * :ref:`Allocate 1 Structure Object using realloc ` * :ref:`Allocate 10 Structure Objects using malloc ` * :ref:`Allocate 10 Structure Objects using calloc ` * :ref:`Allocate 10 Structure Objects using realloc ` * Allocate 10 Structure Objects for structure pointer inside structure * :ref:`Using structure object ` * :ref:`Using structure pointer ` .. _struct_sp_1_structure_malloc: .. tab-set:: .. tab-item:: Allocate 1 Structure Object using malloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of malloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c struct ABC { int a; int b; }; struct ABC *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call malloc() to allocate 1 Structure Object of memory .. code-block:: c ptr = malloc( sizeof(struct ABC) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 1 Structure Object of memory in Heap * ptr can be used to access this 1 Structure Object of memory in heap * In our case sizeof(struct ABC) is equal to 8 Bytes .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created One Structure Object(8 Bytes) in heap ! * Step 4 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to write to memory .. code-block:: c (*ptr).a = 65; (*ptr).b = 66; OR .. code-block:: c ptr[0].a = 65; ptr[0].b = 66; OR .. code-block:: c ptr->a = 65; ptr->b = 66; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]`` and ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]`` and ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 5 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to read from memory .. code-block:: c printf("(*ptr).a = %d\n", (*ptr).a); OR .. code-block:: c printf("ptr[0].a = %d\n", ptr[0].a); OR .. code-block:: c printf("ptr->a = %d\n", ptr->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 6 : Always free the heap memory after usage .. code-block:: c free(ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 12, 14, 17, 20, 23 #include #include struct ABC { int a; int b; }; int main(void) { ptr = malloc(sizeof(struct ABC)); (*ptr).a = 10; printf("(*ptr).a = %d\n", (*ptr).a); ptr[0].a = 20; printf("ptr[0].a = %d\n", ptr[0].a); ptr->a = 30; printf("ptr->a = %d\n", ptr->a); free(ptr); return 0; } .. _struct_sp_1_structure_calloc: .. tab-set:: .. tab-item:: Allocate 1 Structure Object using calloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of calloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c struct ABC { int a; int b; }; struct ABC *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call calloc() to allocate 1 Structure Object of memory .. code-block:: c ptr = calloc( 1, sizeof(struct ABC) ); OR .. code-block:: c ptr = calloc( sizeof(struct ABC), 1 ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 1 Structure Object of memory in Heap * ptr can be used to access this 1 Structure Object of memory in heap * In our case sizeof(struct ABC) is equal to 8 Bytes .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created One Structure Object(8 Bytes) in heap ! * Step 4 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to write to memory .. code-block:: c (*ptr).a = 65; (*ptr).b = 66; OR .. code-block:: c ptr[0].a = 65; ptr[0].b = 66; OR .. code-block:: c ptr->a = 65; ptr->b = 66; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]`` and ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]`` and ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 5 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to read from memory .. code-block:: c printf("(*ptr).a = %d\n", (*ptr).a); OR .. code-block:: c printf("ptr[0].a = %d\n", ptr[0].a); OR .. code-block:: c printf("ptr->a = %d\n", ptr->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 6 : Always free the heap memory after usage .. code-block:: c free(ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 12, 14, 17, 20, 23 #include #include struct ABC { int a; int b; }; int main(void) { ptr = calloc( 1, sizeof(struct ABC) ); (*ptr).a = 10; printf("(*ptr).a = %d\n", (*ptr).a); ptr[0].a = 20; printf("ptr[0].a = %d\n", ptr[0].a); ptr->a = 30; printf("ptr->a = %d\n", ptr->a); free(ptr); return 0; } .. _struct_sp_1_structure_realloc: .. tab-set:: .. tab-item:: Allocate 1 Structure Object using realloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of realloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c struct ABC { int a; int b; }; struct ABC *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call realloc() to allocate 1 Structure Object of memory .. code-block:: c ptr = realloc( NULL, sizeof(struct ABC) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 1 Structure Object of memory in Heap * ptr can be used to access this 1 Structure Object of memory in heap * In our case sizeof(struct ABC) is equal to 8 Bytes .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created One Structure Object(8 Bytes) in heap ! * Step 4 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to write to memory .. code-block:: c (*ptr).a = 65; (*ptr).b = 66; OR .. code-block:: c ptr[0].a = 65; ptr[0].b = 66; OR .. code-block:: c ptr->a = 65; ptr->b = 66; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]`` and ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]`` and ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 5 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to read from memory .. code-block:: c printf("(*ptr).a = %d\n", (*ptr).a); OR .. code-block:: c printf("ptr[0].a = %d\n", ptr[0].a); OR .. code-block:: c printf("ptr->a = %d\n", ptr->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 6 : Always free the heap memory after usage .. code-block:: c free(ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 12, 14, 17, 20, 23 #include #include struct ABC { int a; int b; }; int main(void) { ptr = realloc( NULL, sizeof(struct ABC) ); (*ptr).a = 10; printf("(*ptr).a = %d\n", (*ptr).a); ptr[0].a = 20; printf("ptr[0].a = %d\n", ptr[0].a); ptr->a = 30; printf("ptr->a = %d\n", ptr->a); free(ptr); return 0; } .. tab-set:: .. tab-item:: Summary of 1 Structure Object allocation * malloc, calloc, realloc are memory allocation functions * free is memory deallocation function * stdlib.h contains prototypes of malloc, calloc, realloc, free * ``malloc(sizeof(struct ABC))`` is same as ``calloc(1, sizeof(struct ABC))`` * ``malloc(sizeof(struct ABC))`` is same as ``calloc(sizeof(struct ABC), 1)`` * ``malloc(sizeof(struct ABC))`` is same as ``realloc(NULL, sizeof(struct ABC))`` .. _struct_sp_10_structures_malloc: .. tab-set:: .. tab-item:: Allocate 10 Structure Objects using malloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of malloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c struct ABC { int a; int b; }; struct ABC *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call malloc() to allocate 10 Structure Objects .. code-block:: c ptr = malloc( 10 * sizeof(struct ABC) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 10 Structure Objects(in this case 80 Bytes) of memory in Heap * ptr can be used to access these 10 Structure Objects of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Structure Objects in heap ! * Step 4.1 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to write to First Structure Object in heap memory .. code-block:: c (*ptr).a = 0x11223344; OR .. code-block:: c ptr[0].a = 0x11223344; OR .. code-block:: c ptr->a = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 4.2 : ptr[ ] to write to any allocated Structure in heap memory .. code-block:: c ptr[0].a = 0x11223344; ptr[1].a = 0x55667788; ptr[2].a = 0x99aabbcc; ptr[9].a = 0xddeeff00; * Step 5.1 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to read from first Structure Object in heap memory .. code-block:: c printf("(*ptr).a = %x\n", (*ptr).a); OR .. code-block:: c printf("ptr[0].a = %x\n", ptr[0].a); OR .. code-block:: c printf("ptr->a = %x\n", ptr->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)`` and ``ptr[0]`` and ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 5.2 : ptr[ ] to read from any allocated Structure Object in heap memory .. code-block:: c printf("ptr[0].a = %x\n", ptr[0].a); printf("ptr[1].a = %x\n", ptr[1].a); printf("ptr[2].a = %x\n", ptr[2].a); printf("ptr[9].a = %x\n", ptr[9].a); * Step 6 : Always free the heap memory after usage .. code-block:: c free(ptr); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This frees 10 Structure Objects (80 Bytes) in Heap which was allocated in Step 3 * See full program below .. code-block:: c :linenos: :emphasize-lines: 12, 14, 30 #include #include struct ABC { int a; int b; }; int main(void) { struct ABC *ptr; ptr = malloc( 10 * sizeof(struct ABC) ); (*ptr).a = 0x11223344; printf("(*ptr).a = %x\n", (*ptr).a); ptr[0].a = 0x11223344; ptr[1].a = 0x55667788; ptr[2].a = 0x99aabbcc; ptr[9].a = 0xddeeff00; printf("ptr[0].a = %x\n", ptr[0].a); printf("ptr[1].a = %x\n", ptr[1].a); printf("ptr[2].a = %x\n", ptr[2].a); printf("ptr[9].a = %x\n", ptr[9].a); free(ptr); return 0; } .. _struct_sp_10_structures_calloc: .. tab-set:: .. tab-item:: Allocate 10 Structure Objects using calloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of calloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c struct ABC { int a; int b; }; struct ABC *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call calloc() to allocate 10 Structure Objects .. code-block:: c ptr = calloc( 1, 10 * sizeof(struct ABC) ); OR .. code-block:: c ptr = calloc( 10 * sizeof(struct ABC), 1 ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 10 Structure Objects(in this case 80 Bytes) of memory in Heap * ptr can be used to access these 10 Structure Objects of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Structure Objects in heap ! * Step 4.1 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to write to First Structure Object in heap memory .. code-block:: c (*ptr).a = 0x11223344; OR .. code-block:: c ptr[0].a = 0x11223344; OR .. code-block:: c ptr->a = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 4.2 : ptr[ ] to write to any allocated Structure in heap memory .. code-block:: c ptr[0].a = 0x11223344; ptr[1].a = 0x55667788; ptr[2].a = 0x99aabbcc; ptr[9].a = 0xddeeff00; * Step 5.1 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to read from first Structure Object in heap memory .. code-block:: c printf("(*ptr).a = %x\n", (*ptr).a); OR .. code-block:: c printf("ptr[0].a = %x\n", ptr[0].a); OR .. code-block:: c printf("ptr->a = %x\n", ptr->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)`` and ``ptr[0]`` and ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 5.2 : ptr[ ] to read from any allocated Structure Object in heap memory .. code-block:: c printf("ptr[0].a = %x\n", ptr[0].a); printf("ptr[1].a = %x\n", ptr[1].a); printf("ptr[2].a = %x\n", ptr[2].a); printf("ptr[9].a = %x\n", ptr[9].a); * Step 6 : Always free the heap memory after usage .. code-block:: c free(ptr); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This frees 10 Structure Objects (80 Bytes) in Heap which was allocated in Step 3 * See full program below .. code-block:: c :linenos: :emphasize-lines: 12, 14, 30 #include #include struct ABC { int a; int b; }; int main(void) { struct ABC *ptr; ptr = calloc( 1, 10 * sizeof(struct ABC) ); (*ptr).a = 0x11223344; printf("(*ptr).a = %x\n", (*ptr).a); ptr[0].a = 0x11223344; ptr[1].a = 0x55667788; ptr[2].a = 0x99aabbcc; ptr[9].a = 0xddeeff00; printf("ptr[0].a = %x\n", ptr[0].a); printf("ptr[1].a = %x\n", ptr[1].a); printf("ptr[2].a = %x\n", ptr[2].a); printf("ptr[9].a = %x\n", ptr[9].a); free(ptr); return 0; } .. _struct_sp_10_structures_realloc: .. tab-set:: .. tab-item:: Allocate 10 Structure Objects using realloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of realloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c struct ABC { int a; int b; }; struct ABC *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call malloc() to allocate 10 Structure Objects .. code-block:: c ptr = realloc( NULL, 10 * sizeof(struct ABC) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 10 Structure Objects(in this case 80 Bytes) of memory in Heap * ptr can be used to access these 10 Structure Objects of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Structure Objects in heap ! * Step 4.1 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to write to First Structure Object in heap memory .. code-block:: c (*ptr).a = 0x11223344; OR .. code-block:: c ptr[0].a = 0x11223344; OR .. code-block:: c ptr->a = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 4.2 : ptr[ ] to write to any allocated Structure in heap memory .. code-block:: c ptr[0].a = 0x11223344; ptr[1].a = 0x55667788; ptr[2].a = 0x99aabbcc; ptr[9].a = 0xddeeff00; * Step 5.1 : Use ``(*ptr)``, ``ptr[0]``, ``ptr->`` to read from first Structure Object in heap memory .. code-block:: c printf("(*ptr).a = %x\n", (*ptr).a); OR .. code-block:: c printf("ptr[0].a = %x\n", ptr[0].a); OR .. code-block:: c printf("ptr->a = %x\n", ptr->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)`` and ``ptr[0]`` and ``ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*ptr)``, ``ptr[0]``, ``ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 5.2 : ptr[ ] to read from any allocated Structure Object in heap memory .. code-block:: c printf("ptr[0].a = %x\n", ptr[0].a); printf("ptr[1].a = %x\n", ptr[1].a); printf("ptr[2].a = %x\n", ptr[2].a); printf("ptr[9].a = %x\n", ptr[9].a); * Step 6 : Always free the heap memory after usage .. code-block:: c free(ptr); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This frees 10 Structure Objects (80 Bytes) in Heap which was allocated in Step 3 * See full program below .. code-block:: c :linenos: :emphasize-lines: 12, 14, 30 #include #include struct ABC { int a; int b; }; int main(void) { struct ABC *ptr; ptr = realloc( NULL, 10 * sizeof(struct ABC) ); (*ptr).a = 0x11223344; printf("(*ptr).a = %x\n", (*ptr).a); ptr[0].a = 0x11223344; ptr[1].a = 0x55667788; ptr[2].a = 0x99aabbcc; ptr[9].a = 0xddeeff00; printf("ptr[0].a = %x\n", ptr[0].a); printf("ptr[1].a = %x\n", ptr[1].a); printf("ptr[2].a = %x\n", ptr[2].a); printf("ptr[9].a = %x\n", ptr[9].a); free(ptr); return 0; } .. _struct_sp_10_structures_structure_object: .. tab-set:: .. tab-item:: Allocate 10 Structure Objects for structure pointer inside structure : Using structure object * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of malloc() is present in stdlib.h * Step 2 : Define a structure having a character pointer as a member .. code-block:: c struct ABC { int a; int b; }; struct XYZ { struct ABC *ptr; }; * Step 3 : Define a variable of structure .. code-block:: c struct XYZ X; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(X.ptr) is 8 Bytes * Step 4 : Call malloc() to allocate 10 Structure Objects .. code-block:: c X.ptr = malloc( 10 * sizeof(struct ABC) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 10 Structure Objects(80 Bytes) of memory in Heap * X.ptr can be used to access these 10 Structure Objects(80 Bytes) of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Structure Objects in heap ! * Step 5.1 : Use ``(*X.ptr)`` or ``X.ptr[0]`` or ``X.ptr->`` to write to first Structure Object in heap .. code-block:: c (*X.ptr).a = 0x11223344; OR .. code-block:: c X.ptr[0].a = 0x11223344; OR .. code-block:: c X.ptr->a = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*X.ptr)`` and ``X.ptr[0]`` and ``X.ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*X.ptr)``, ``X.ptr[0]``, ``X.ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 5.2 : Use X.ptr[0], X.ptr[1] ... X.ptr[9] to write to allocated Structure Objects in Heap .. code-block:: c X.ptr[0].a = 0x11223344; X.ptr[1].a = 0x55667788; X.ptr[2].a = 0x99aabbcc; X.ptr[9].a = 0xddeeff00; * Step 6.1 : Use ``(*X.ptr)`` or ``X.ptr[0]`` or ``X.ptr->`` to read from first Structure Object from heap .. code-block:: c printf("(*X.ptr).a = %d\n", (*X.ptr).a); OR .. code-block:: c printf("X.ptr[0].a = %d\n", X.ptr[0].a); OR .. code-block:: c printf("X.ptr->a = %d\n", X.ptr->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*X.ptr)`` and ``X.ptr[0]`` and ``X.ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*X.ptr)``, ``X.ptr[0]``, ``X.ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 6.2 : Use X.ptr[0], X.ptr[1] ... X.ptr[9] to read from allocated Structure Objects in Heap .. code-block:: c printf("X.ptr[0].a = %d\n", X.ptr[0].a); printf("X.ptr[1].a = %d\n", X.ptr[1].a); printf("X.ptr[2].a = %d\n", X.ptr[2].a); printf("X.ptr[9].a = %d\n", X.ptr[9].a); * Step 7 : Always free the heap memory after usage .. code-block:: c free(X.ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 19, 21, 24, 27, 40 #include #include struct ABC { int a; int b; }; struct XYZ { struct ABC *ptr; }; int main(void) { struct XYZ X; X.ptr = malloc( 10 * sizeof(struct ABC) ); (*X.ptr).a = 0x11223344; printf("(*X.ptr).a = %x\n", (*X.ptr).a); X.ptr[0].a = 0x11223344; printf("X.ptr[0].a = %x\n", X.ptr[0].a); X.ptr->a = 0x11223344; printf("X.ptr->a = %x\n", X.ptr->a); X.ptr[0].a = 0x11223344; X.ptr[1].a = 0x55667788; X.ptr[2].a = 0x99aabbcc; X.ptr[9].a = 0xddeeff00; printf("X.ptr[0].a = %x\n", X.ptr[0].a); printf("X.ptr[1].a = %x\n", X.ptr[1].a); printf("X.ptr[2].a = %x\n", X.ptr[2].a); printf("X.ptr[9].a = %x\n", X.ptr[9].a); free(X.ptr); return 0; } .. _struct_sp_10_structures_structure_pointer: .. tab-set:: .. tab-item:: Allocate 10 Structure Objects for structure pointer inside structure : Using structure pointer * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of malloc() is present in stdlib.h * Step 2 : Define a structure having a character pointer as a member .. code-block:: c struct ABC { int a; int b; }; struct XYZ { struct ABC *ptr; }; * Step 3 : Define a variable of structure .. code-block:: c struct XYZ X; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(X.ptr) is 8 Bytes * Step 4 : Define a pointer to X .. code-block:: c struct XYZ *P; P = &X; * Step 4 : Call malloc() to allocate 10 Structure Objects .. code-block:: c P->ptr = malloc( 10 * sizeof(struct ABC) ); OR .. code-block:: c P[0].ptr = malloc( 10 * sizeof(struct ABC) ); OR .. code-block:: c (*P).ptr = malloc( 10 * sizeof(struct ABC) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 10 Structure Objects(80 Bytes) of memory in Heap * P->ptr, P[0].ptr, (\*P).ptr can be used to access these 10 Structure Objects(80 Bytes) of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Structure Objects in heap ! * Step 5.1 : Use ``P`` to write to first Structure Object in heap .. code-block:: c P->ptr->a = 0x11223344; OR .. code-block:: c P->ptr[0].a = 0x11223344; OR .. code-block:: c (*P->ptr).a = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*P->ptr)`` and ``P->ptr[0]`` and ``P->ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*P->ptr)`` and ``P->ptr[0]`` and ``P->ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 5.2 : Use P->ptr[0], P->ptr[1] ... P->ptr[9] to write to allocated Structure Objects in Heap .. code-block:: c P->ptr[0].a = 0x11223344; P->ptr[1].a = 0x55667788; P->ptr[2].a = 0x99aabbcc; P->ptr[9].a = 0xddeeff00; * Step 6.1 : Use ``(*P->ptr)`` and ``P->ptr[0]`` and ``P->ptr->`` to read from first Structure Object from heap .. code-block:: c printf("(*P->ptr).a = %d\n", (*P->ptr).a); OR .. code-block:: c printf("P->ptr[0].a = %d\n", P->ptr[0].a); OR .. code-block:: c printf("P->ptr->a = %d\n", P->ptr->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*P->ptr)`` and ``P->ptr[0]`` and ``P->ptr->`` can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``(*P->ptr)`` and ``P->ptr[0]`` and ``P->ptr->`` are the ways to access memory. It is called dereferencing of pointer formally * Step 6.2 : Use P->ptr[0], P->ptr[1] ... P->ptr[9] to read from allocated Structure Objects in Heap .. code-block:: c printf("P->ptr[0].a = %x\n", P->ptr[0].a); printf("P->ptr[1].a = %x\n", P->ptr[1].a); printf("P->ptr[2].a = %x\n", P->ptr[2].a); printf("P->ptr[9].a = %x\n", P->ptr[9].a); * Step 7 : Always free the heap memory after usage .. code-block:: c free(X.ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 19, 21, 23, 25, 28, 31, 44 #include #include struct ABC { int a; int b; }; struct XYZ { struct ABC *ptr; }; int main(void) { struct XYZ X; struct XYZ *P; P = &X; P->ptr = malloc( 10 * sizeof(struct ABC) ); (*P->ptr).a = 0x11223344; printf("(*P->ptr).a = %x\n", (*P->ptr).a); P->ptr[0].a = 0x11223344; printf("P->ptr[0].a = %x\n", P->ptr[0].a); P->ptr->a = 0x11223344; printf("P->ptr->a = %x\n", P->ptr->a); P->ptr[0].a = 0x11223344; P->ptr[1].a = 0x55667788; P->ptr[2].a = 0x99aabbcc; P->ptr[9].a = 0xddeeff00; printf("P->ptr[0].a = %x\n", P->ptr[0].a); printf("P->ptr[1].a = %x\n", P->ptr[1].a); printf("P->ptr[2].a = %x\n", P->ptr[2].a); printf("P->ptr[9].a = %x\n", P->ptr[9].a); free(X.ptr); return 0; } .. card:: See Also * Current Module * :doc:`../malloc_ptr` * Previous Module * :doc:`../../array_n_ptrs/array_n_ptrs` * Next Module * :doc:`../../typecasting_n_ptr/typecasting_n_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../funcs_n_ptrs/funcs_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`