Memcpy structure 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 do memcpy with structure single pointer ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Single structure copy ` * :ref:`Single structure copy using memcpy ` * :ref:`Single structure copy using pointers ` * :ref:`Single structure copy using pointers with memcpy ` * :ref:`Single structure copy using pointers indexing and loop ` * :ref:`Single structure copy using pointers increment and loop ` * :ref:`Two Arrays copy : One structure at a time with array indexing : Loop ` * :ref:`Two Arrays copy : One structure at a time with array indexing (pointer notation): Loop ` * :ref:`Two Arrays copy : One structure at a time with pointers indexing : Loop ` * :ref:`Two Arrays copy : One structure at a time with pointers incrementing : Loop ` * :ref:`Two Arrays copy : memcpy using Array Names ` * :ref:`Two Arrays copy : memcpy using pointer alias Names ` * :ref:`Two Arrays copy : memcpy using Array Names : Relative position copy ` * :ref:`Two Arrays copy : Extract part of array into another array ` * :ref:`Single array : Change contents of Single array using a Single pointer ` * :ref:`Single array : Change contents of Single array using memcpy ` * :ref:`Use of standard strcpy on heap memory ` * :ref:`Custom memcpy with single pointer : Pass single pointer to a function : Call by Value ` * :ref:`Custom memcpy with single pointer : Pass single pointer to a function : Call by Reference ` .. _sp_memcpy_ptr_struct_sp_ex_1: .. tab-set:: .. tab-item:: Single structure copy * Step 1 : Define two structure variables .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; * Step 2 : Do structure copy .. code-block:: c b = a; * Step 3 : Print structures .. code-block:: c printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); * See full program below .. code-block:: c :linenos: :emphasize-lines: 8 #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; b = a; printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); return 0; } * Output is as below .. code-block:: c a.x = 10, a.y = 20, a.z = 30 .. _sp_memcpy_ptr_struct_sp_ex_2: .. tab-set:: .. tab-item:: Single structure copy using `memcpy` * Step 1 : Define two structure variables .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; * Step 2 : Do structure copy .. code-block:: c memcpy(&b, &a, sizeof(struct ABC)); * Step 3 : Print structures .. code-block:: c printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); * See full program below .. code-block:: c :linenos: :emphasize-lines: 9 #include #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; memcpy(&b, &a, sizeof(struct ABC)); printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); return 0; } * Output is as below .. code-block:: c a.x = 10, a.y = 20, a.z = 30 .. _sp_memcpy_ptr_struct_sp_ex_3: .. tab-set:: .. tab-item:: Single structure copy using pointers * Step 1 : Define two structure variables .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; * Step 2 : Define two pointers .. code-block:: c struct ABC *p; struct ABC *q; p = &a; q = &b; * Step 3 : Do structure copy .. code-block:: c *q = *p; * Step 4 : Print structures .. code-block:: c printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); * See full program below .. code-block:: c :linenos: :emphasize-lines: 11,12 #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; struct ABC *p; struct ABC *q; p = &a; q = &b; *q = *p; printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); return 0; } * Output is as below .. code-block:: c a.x = 10, a.y = 20, a.z = 30 .. _sp_memcpy_ptr_struct_sp_ex_4: .. tab-set:: .. tab-item:: Single structure copy using pointers with memcpy * Step 1 : Define two structure variables .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; * Step 2 : Define two pointers .. code-block:: c struct ABC *p; struct ABC *q; p = &a; q = &b; * Step 3 : Do structure copy using memcpy .. code-block:: c memcpy(q, p, sizeof(struct ABC)); * Step 4 : Print structures .. code-block:: c printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); * See full program below .. code-block:: c :linenos: :emphasize-lines: 15 #include #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; struct ABC *p; struct ABC *q; p = &a; q = &b; memcpy(q, p, sizeof(struct ABC)); printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); return 0; } * Output is as below .. code-block:: c a.x = 10, a.y = 20, a.z = 30 .. _sp_memcpy_ptr_struct_sp_ex_5: .. tab-set:: .. tab-item:: Single structure copy using pointers indexing and loop * Step 1 : Define two structure variables .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; * Step 2 : Define two pointers .. code-block:: c struct ABC *p; struct ABC *q; p = &a; q = &b; * Step 3 : Do structure copy using memcpy .. code-block:: c for (int i = 0; i < sizeof(struct ABC); i += sizeof(struct ABC)) { q[i] = p[i]; } * Step 4 : Print structures .. code-block:: c printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); * See full program below .. code-block:: c :linenos: :emphasize-lines: 14, 15, 16, 17 #include struct ABC { int x; int y; int z; }; struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; int main(void) { struct ABC *p; struct ABC *q; p = &a; q = &b; for (int i = 0; i < sizeof(struct ABC); i += sizeof(struct ABC)) { q[i] = p[i]; } printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); return 0; } * Output is as below .. code-block:: c a.x = 10, a.y = 20, a.z = 30 .. _sp_memcpy_ptr_struct_sp_ex_6: .. tab-set:: .. tab-item:: Single structure copy using pointers increment and loop * Step 1 : Define two structure variables .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; * Step 2 : Define two pointers .. code-block:: c struct ABC *p; struct ABC *q; p = &a; q = &b; * Step 3 : Do structure copy using memcpy .. code-block:: c for (int i = 0; i < sizeof(struct ABC); i += sizeof(struct ABC) ) { *q++ = *p++; } * Step 4 : Print structures .. code-block:: c printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); * See full program below .. code-block:: c :linenos: :emphasize-lines: 14, 15, 16, 17 #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a = { .x = 10, .y = 20, .z = 30 }; struct ABC b; struct ABC *p; struct ABC *q; p = &a; q = &b; for (int i = 0; i < sizeof(struct ABC); i += sizeof(struct ABC) ) { *q++ = *p++; } printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z ); return 0; } * Output is as below .. code-block:: c a.x = 10, a.y = 20, a.z = 30 .. _sp_memcpy_ptr_struct_sp_ex_7: .. tab-set:: .. tab-item:: Two Arrays copy : One structure at a time with array indexing : Loop * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; * Step 2 : Do copy .. code-block:: c for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { b[i] = a[i]; } * Step 3 : Print destination array .. code-block:: c for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } * See full program below .. code-block:: c :linenos: :emphasize-lines: 10, 15 #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { b[i] = a[i]; } for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c b[0].x = 1 b[0].y = 2 b[0].z = 3 b[1].x = 10 b[1].y = 20 b[1].z = 30 .. _sp_memcpy_ptr_struct_sp_ex_8: .. tab-set:: .. tab-item:: Two Arrays copy : One structure at a time with array indexing (pointer notation): Loop * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; * Step 2 : Do copy .. code-block:: c for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { *(b + i) = *(a + i); } * Step 3 : Print destination array .. code-block:: c for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("*(b + %d).x = %d ", i, (*(b + i)).x ); printf("*(b + %d).y = %d ", i, (*(b + i)).y ); printf("*(b + %d).z = %d ", i, (*(b + i)).z ); printf("\n"); } * See full program below .. code-block:: c :linenos: :emphasize-lines: 10, 15 #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { *(b + i) = *(a + i); } for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("*(b + %d).x = %d ", i, (*(b + i)).x ); printf("*(b + %d).y = %d ", i, (*(b + i)).y ); printf("*(b + %d).z = %d ", i, (*(b + i)).z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c *(b + 0).x = 1 *(b + 0).y = 2 *(b + 0).z = 3 *(b + 1).x = 10 *(b + 1).y = 20 *(b + 1).z = 30 .. _sp_memcpy_ptr_struct_sp_ex_9: .. tab-set:: .. tab-item:: Two Arrays copy : One structure at a time with pointers indexing : Loop * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; * Step 2 : Define two pointers .. code-block:: c struct ABC *p; struct ABC *q; p = a; q = b; * Step 3 : Do copy .. code-block:: c for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { q[i] = p[i]; } * See full program below .. code-block:: c :linenos: :emphasize-lines: 16 #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; struct ABC *p; struct ABC *q; p = a; q = b; for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { q[i] = p[i]; } for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c b[0].x = 1 b[0].y = 2 b[0].z = 3 b[1].x = 10 b[1].y = 20 b[1].z = 30 .. _sp_memcpy_ptr_struct_sp_ex_10: .. tab-set:: .. tab-item:: Two Arrays copy : One structure at a time with pointers incrementing : Loop * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; * Step 2 : Define two pointers .. code-block:: c struct ABC *p; struct ABC *q; p = a; q = b; * Step 3 : Do copy .. code-block:: c for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { *q++ = *p++; } * See full program below .. code-block:: c :linenos: :emphasize-lines: 16 #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; struct ABC *p; struct ABC *q; p = a; q = b; for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { *q++ = *p++; } for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c b[0].x = 1 b[0].y = 2 b[0].z = 3 b[1].x = 10 b[1].y = 20 b[1].z = 30 .. _sp_memcpy_ptr_struct_sp_ex_11: .. tab-set:: .. tab-item:: Two Arrays copy : memcpy using Array Names * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; * Step 2 : Do copy .. code-block:: c memcpy(b, a, sizeof(b)); * See full program below .. code-block:: c :linenos: :emphasize-lines: 9 #include #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; memcpy(b, a, sizeof(b)); for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c b[0].x = 1 b[0].y = 2 b[0].z = 3 b[1].x = 10 b[1].y = 20 b[1].z = 30 .. _sp_memcpy_ptr_struct_sp_ex_12: .. tab-set:: .. tab-item:: Two Arrays copy : memcpy using pointer alias Names * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; * Step 2 : Define Pointers .. code-block:: c struct ABC *p; struct ABC *q; p = a; q = b; * Step 3 : Do copy .. code-block:: c memcpy(q, p, sizeof(b)); * See full program below .. code-block:: c :linenos: :emphasize-lines: 15 #include #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[2] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, }; struct ABC b[2]; struct ABC *p; struct ABC *q; p = a; q = b; memcpy(q, p, sizeof(b)); for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c b[0].x = 1 b[0].y = 2 b[0].z = 3 b[1].x = 10 b[1].y = 20 b[1].z = 30 .. _sp_memcpy_ptr_struct_sp_ex_13: .. tab-set:: .. tab-item:: Two Arrays copy : memcpy using Array Names : Relative position copy * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4]; * Step 2 : Use first 2 structures in array "b" .. code-block:: c b[0].x = 11; b[0].y = 22; b[0].z = 33; b[1].x = 44; b[1].y = 55; b[1].z = 66; * Step 3 : Use remainig 2 structures in array "b" to copy contents from array "a" .. code-block:: c memcpy(b + 2, a + 2, sizeof(b) - (2 * sizeof(struct ABC)) ); * Step 4 : Print the contents of array "b" .. code-block:: c for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } * See full program below .. code-block:: c :linenos: #include #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4]; // Clear garbage contents in array "b" memset(b, 0, sizeof(b)); b[0].x = 11; b[0].y = 22; b[0].z = 33; b[1].x = 44; b[1].y = 55; b[1].z = 66; memcpy(b + 2, a + 2, sizeof(b) - (2 * sizeof(struct ABC)) ); for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c b[0].x = 11 b[0].y = 22 b[0].z = 33 b[1].x = 44 b[1].y = 55 b[1].z = 66 b[2].x = 100 b[2].y = 200 b[2].z = 300 b[3].x = 1000 b[3].y = 2000 b[3].z = 3000 .. _sp_memcpy_ptr_struct_sp_ex_14: .. tab-set:: .. tab-item:: Two Arrays copy : Extract part of array into another array * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4]; * Step 2 : Define two pointers .. code-block:: c struct ABC *p; struct ABC *q; * Step 3 : Extract part of data from array "a" into array "b" .. code-block:: c p = a + 1; q = a + 2; for (int i = 0; p <= q; i++) { b[i] = *p; p++; } * Step 4 : Print the contents of array "b" .. code-block:: c for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } * See full program below .. code-block:: c :linenos: #include #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4]; struct ABC *p; struct ABC *q; // Clear garbage contents in array "b" memset(b, 0, sizeof(b)); // Extract part of data from array "a" into array "b" p = a + 1; q = a + 2; for (int i = 0; p <= q; i++) { b[i] = *p; p++; } for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c b[0].x = 10 b[0].y = 20 b[0].z = 30 b[1].x = 100 b[1].y = 200 b[1].z = 300 b[2].x = 0 b[2].y = 0 b[2].z = 0 b[3].x = 0 b[3].y = 0 b[3].z = 0 .. _sp_memcpy_ptr_struct_sp_ex_15: .. tab-set:: .. tab-item:: Single array : Change contents of Single array using a Single pointer * Step 1 : Define a Single Dimension array .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; * Step 2 : Define a Single pointer .. code-block:: c struct ABC *p; * Step 3 : Change part of the array using a single pointer .. code-block:: c p = a + 2; // p[0] is equal to a[2] p[0].x = 11; p[0].y = 22; p[0].z = 33; // p[1] is equal to a[3] p[1].x = 44; p[1].y = 55; p[1].z = 66; * Step 4 : Print the contents of array "a" .. code-block:: c for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { printf("a[%d].x = %d ", i, a[i].x ); printf("a[%d].y = %d ", i, a[i].y ); printf("a[%d].z = %d ", i, a[i].z ); printf("\n"); } * See full program below .. code-block:: c :linenos: #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC *p; // Change part of the array using a single pointer p = a + 2; // p[0] is equal to a[2] p[0].x = 11; p[0].y = 22; p[0].z = 33; // p[1] is equal to a[3] p[1].x = 44; p[1].y = 55; p[1].z = 66; for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { printf("a[%d].x = %d ", i, a[i].x ); printf("a[%d].y = %d ", i, a[i].y ); printf("a[%d].z = %d ", i, a[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c a[0].x = 1 a[0].y = 2 a[0].z = 3 a[1].x = 10 a[1].y = 20 a[1].z = 30 a[2].x = 11 a[2].y = 22 a[2].z = 33 a[3].x = 44 a[3].y = 55 a[3].z = 66 .. _sp_memcpy_ptr_struct_sp_ex_16: .. tab-set:: .. tab-item:: Single array : Change contents of Single array using memcpy * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 11, .y = 22, .z = 33}, { .x = 44, .y = 55, .z = 66}, { .x = 77, .y = 88, .z = 99}, }; * Step 2 : Change part of array "a" by copying contents from array "b" .. code-block:: c memcpy(a + 1, b + 1, 3 * sizeof(struct ABC)); * Step 3 : Print the contents of array "a" .. code-block:: c for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { printf("a[%d].x = %d ", i, a[i].x ); printf("a[%d].y = %d ", i, a[i].y ); printf("a[%d].z = %d ", i, a[i].z ); printf("\n"); } * See full program below .. code-block:: c :linenos: #include #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 11, .y = 22, .z = 33}, { .x = 44, .y = 55, .z = 66}, { .x = 77, .y = 88, .z = 99}, }; // Change part of array "a" by copying contents from array "b" memcpy(a + 1, b + 1, 3 * sizeof(struct ABC)); // Print the contents of array "a" for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { printf("a[%d].x = %d ", i, a[i].x ); printf("a[%d].y = %d ", i, a[i].y ); printf("a[%d].z = %d ", i, a[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c a[0].x = 1 a[0].y = 2 a[0].z = 3 a[1].x = 11 a[1].y = 22 a[1].z = 33 a[2].x = 44 a[2].y = 55 a[2].z = 66 a[3].x = 77 a[3].y = 88 a[3].z = 99 .. _sp_memcpy_ptr_struct_sp_ex_17: .. tab-set:: .. tab-item:: Use of standard memcpy on heap memory * Step 1 : Define an array and a pointer .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC *q; * Step 2 : Allocate heap memory to pointer .. code-block:: c q = malloc(4 * sizeof(struct ABC)); * Step 3 : Clear garbage contents of allocated heap memory .. code-block:: c memset(q, 0, 4 * sizeof(struct ABC)); * Step 4 : Use standard "memcpy" on heap memory .. code-block:: c memcpy(q, a, 4 * sizeof(struct ABC)); * Step 5 : Free heap memory after use .. code-block:: c free(q); * See full program below .. code-block:: c :linenos: #include #include #include struct ABC { int x; int y; int z; }; int main(void) { struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC *q; // Allocate 10 bytes memory in heap. Let "q" point to it q = malloc(4 * sizeof(struct ABC)); // Clear garbage contents of allocated heap memory memset(q, 0, 4 * sizeof(struct ABC)); // Use standard "memcpy" to copy contents into heap memory pointed by "q" memcpy(q, a, 4 * sizeof(struct ABC)); // Use "%d" to print contents heap memory pointed by "q" for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { printf("q[%d].x = %d ", i, q[i].x ); printf("q[%d].y = %d ", i, q[i].y ); printf("q[%d].z = %d ", i, q[i].z ); printf("\n"); } printf("\n"); free(q); } * Output is as below .. code-block:: c q[0].x = 1 q[0].y = 2 q[0].z = 3 q[1].x = 10 q[1].y = 20 q[1].z = 30 q[2].x = 100 q[2].y = 200 q[2].z = 300 q[3].x = 1000 q[3].y = 2000 q[3].z = 3000 .. _sp_memcpy_ptr_struct_sp_ex_18: .. tab-set:: .. tab-item:: Custom memcpy with single pointer : Pass single pointer to a function : Call by Value * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4]; * Step 2 : Define two pointers .. code-block:: c struct ABC *p; struct ABC *q; p = a; q = b; * Step 3 : Pass single pointer to a function : Call by Value .. code-block:: c my_memcpy_1(q, p, sizeof(b)); * Step 4 : Define `my_memcpy_1` function .. code-block:: c void my_memcpy_1(struct ABC *dest, struct ABC *src, int size) { memcpy(dest, src, size); } * See full program below .. code-block:: c :linenos: #include #include struct ABC { int x; int y; int z; }; void my_memcpy_1(struct ABC *dest, struct ABC *src, int size) { memcpy(dest, src, size); } int main(void) { struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4]; struct ABC *p; struct ABC *q; p = a; q = b; memset(b, 0, sizeof(b)); my_memcpy_1(q, p, sizeof(b)); // Use "%d" to print contents heap memory pointed by "q" for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c b[0].x = 1 b[0].y = 2 b[0].z = 3 b[1].x = 10 b[1].y = 20 b[1].z = 30 b[2].x = 100 b[2].y = 200 b[2].z = 300 b[3].x = 1000 b[3].y = 2000 b[3].z = 3000 .. _sp_memcpy_ptr_struct_sp_ex_19: .. tab-set:: .. tab-item:: Custom memcpy with single pointer : Pass single pointer to a function : Call by Reference * Step 1 : Define two arrays .. code-block:: c struct ABC { int x; int y; int z; }; struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4]; * Step 2 : Define two pointers .. code-block:: c struct ABC *p; struct ABC *q; p = a; q = b; * Step 3 : Pass single pointer to a function : Call by Value .. code-block:: c my_memcpy_2(q, p, sizeof(b)); * Step 4 : Define `my_memcpy_1` function .. code-block:: c void my_memcpy_2(struct ABC **dest, struct ABC **src, int size) { memcpy(*dest, *src, size); } * See full program below .. code-block:: c :linenos: #include #include struct ABC { int x; int y; int z; }; void my_memcpy_2(struct ABC **dest, struct ABC **src, int size) { memcpy(*dest, *src, size); } int main(void) { struct ABC a[4] = { { .x = 1, .y = 2, .z = 3}, { .x = 10, .y = 20, .z = 30}, { .x = 100, .y = 200, .z = 300}, { .x = 1000, .y = 2000, .z = 3000}, }; struct ABC b[4]; struct ABC *p; struct ABC *q; p = a; q = b; memset(b, 0, sizeof(b)); my_memcpy_2(&q, &p, sizeof(b)); // Use "%d" to print contents heap memory pointed by "q" for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("b[%d].x = %d ", i, b[i].x ); printf("b[%d].y = %d ", i, b[i].y ); printf("b[%d].z = %d ", i, b[i].z ); printf("\n"); } printf("\n"); return 0; } * Output is as below .. code-block:: c b[0].x = 1 b[0].y = 2 b[0].z = 3 b[1].x = 10 b[1].y = 20 b[1].z = 30 b[2].x = 100 b[2].y = 200 b[2].z = 300 b[3].x = 1000 b[3].y = 2000 b[3].z = 3000 .. card:: See Also * Current Module * :doc:`../memcpy_ptr` * Previous Module * :doc:`../../funcs_n_ptrs/funcs_n_ptrs` * Next Module * :doc:`../../const_ptr/const_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../array_n_ptrs/array_n_ptrs` * :doc:`../../malloc_ptr/malloc_ptr` * :doc:`../../typecasting_n_ptr/typecasting_n_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`