Memcpy int 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 integer single pointer ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Single integer copy ` * :ref:`Single integer copy using memcpy ` * :ref:`Single integer copy using pointers ` * :ref:`Single integer copy using pointers with memcpy ` * :ref:`Single integer copy using pointers indexing and loop ` * :ref:`Single integer copy using pointers increment and loop ` * :ref:`Two Arrays copy : One int at a time with array indexing : Loop ` * :ref:`Two Arrays copy : One int at a time with array indexing (pointer notation): Loop ` * :ref:`Two Arrays copy : One int at a time with pointers indexing : Loop ` * :ref:`Two Arrays copy : One int 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 memcpy 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_int_sp_ex_1: .. tab-set:: .. tab-item:: Single integer copy * Step 1 : Define two integer variables .. code-block:: c int a = 5; int b; * Step 2 : Do integer copy .. code-block:: c b = a; * Step 3 : Print integers .. code-block:: c printf("a = %d, b = %d\n", a, b); * See full program below .. code-block:: c :linenos: :emphasize-lines: 8 #include int main(void) { int a = 5; int b; b = a; printf("a = %d, b = %d\n", a, b); return 0; } * Output is as below .. code-block:: c a = 5, b = 5 .. _sp_memcpy_ptr_int_sp_ex_2: .. tab-set:: .. tab-item:: Single integer copy using `memcpy` * Step 1 : Define two integer variables .. code-block:: c int a = 5; int b; * Step 2 : Do integer copy .. code-block:: c memcpy(&b, &a, sizeof(int)); * Step 3 : Print integers .. code-block:: c printf("a = %d, b = %d\n", a, b); * See full program below .. code-block:: c :linenos: :emphasize-lines: 9 #include #include int main(void) { int a = 5; int b; memcpy(&b, &a, sizeof(int)); printf("a = %d, b = %d\n", a, b); return 0; } * Output is as below .. code-block:: c a = 5, b = 5 .. _sp_memcpy_ptr_int_sp_ex_3: .. tab-set:: .. tab-item:: Single integer copy using pointers * Step 1 : Define two integer variables .. code-block:: c int a = 5; int b; * Step 2 : Define two pointers .. code-block:: c int *p; int *q; p = &a; q = &b; * Step 3 : Do integer copy .. code-block:: c *q = *p; * Step 4 : Print integers .. code-block:: c printf("a = %d, b = %d\n", a, b); * See full program below .. code-block:: c :linenos: :emphasize-lines: 11,12 #include int main(void) { int a = 5; int b; int *p; int *q; p = &a; q = &b; *q = *p; printf("a = %d, b = %d\n", a, b); return 0; } * Output is as below .. code-block:: c a = 5, b = 5 .. _sp_memcpy_ptr_int_sp_ex_4: .. tab-set:: .. tab-item:: Single integer copy using pointers with memcpy * Step 1 : Define two integer variables .. code-block:: c int a = 5; int b; * Step 2 : Define two pointers .. code-block:: c int *p; int *q; p = &a; q = &b; * Step 3 : Do integer copy using memcpy .. code-block:: c memcpy(q, p, sizeof(int)); * Step 4 : Print integers .. code-block:: c printf("a = %d, b = %d\n", a, b); * See full program below .. code-block:: c :linenos: :emphasize-lines: 15 #include #include int main(void) { int a = 5; int b; int *p; int *q; p = &a; q = &b; memcpy(q, p, sizeof(int)); printf("a = %d, b = %d\n", a, b); return 0; } * Output is as below .. code-block:: c a = 5, b = 5 .. _sp_memcpy_ptr_int_sp_ex_5: .. tab-set:: .. tab-item:: Single integer copy using pointers indexing and loop * Step 1 : Define two integer variables .. code-block:: c int a = 5; int b; * Step 2 : Define two pointers .. code-block:: c int *p; int *q; p = &a; q = &b; * Step 3 : Do integer copy using memcpy .. code-block:: c for (int i = 0; i < sizeof(int); i += 4) { q[i] = p[i]; } * Step 4 : Print integers .. code-block:: c printf("a = %d, b = %d\n", a, b); * See full program below .. code-block:: c :linenos: :emphasize-lines: 14, 15, 16, 17 #include int main(void) { int a = 5; int b; int *p; int *q; p = &a; q = &b; for (int i = 0; i < sizeof(int); i += 4) { q[i] = p[i]; } printf("a = %d, b = %d\n", a, b); return 0; } * Output is as below .. code-block:: c a = 5, b = 5 .. _sp_memcpy_ptr_int_sp_ex_6: .. tab-set:: .. tab-item:: Single integer copy using pointers increment and loop * Step 1 : Define two integer variables .. code-block:: c int a = 5; int b; * Step 2 : Define two pointers .. code-block:: c int *p; int *q; p = &a; q = &b; * Step 3 : Do integer copy using memcpy .. code-block:: c for (int i = 0; i < sizeof(int); i += 4) { *q++ = *p++; } * Step 4 : Print integers .. code-block:: c printf("a = %d, b = %d\n", a, b); * See full program below .. code-block:: c :linenos: :emphasize-lines: 14, 15, 16, 17 #include int main(void) { int a = 5; int b; int *p; int *q; p = &a; q = &b; for (int i = 0; i < sizeof(int); i += 4) { *q++ = *p++; } printf("a = %d, b = %d\n", a, b); return 0; } * Output is as below .. code-block:: c a = 5, b = 5 .. _sp_memcpy_ptr_int_sp_ex_7: .. tab-set:: .. tab-item:: Two Arrays copy : One int at a time with array indexing : Loop * Step 1 : Define two arrays .. code-block:: c int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * 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("%d ", b[i]); } * See full program below .. code-block:: c :linenos: :emphasize-lines: 10, 15 #include int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; 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("%d ", b[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 4 5 6 7 8 9 10 .. _sp_memcpy_ptr_int_sp_ex_8: .. tab-set:: .. tab-item:: Two Arrays copy : One int at a time with array indexing (pointer notation): Loop * Step 1 : Define two arrays .. code-block:: c int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * 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("%d ", *(b + i) ); } * See full program below .. code-block:: c :linenos: :emphasize-lines: 10, 15 #include int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; 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("%d ", *(b + i) ); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 4 5 6 7 8 9 10 .. _sp_memcpy_ptr_int_sp_ex_9: .. tab-set:: .. tab-item:: Two Arrays copy : One int at a time with pointers indexing : Loop * Step 1 : Define two arrays .. code-block:: c int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * Step 2 : Define two pointers .. code-block:: c int *p; int *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 int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; int *p; int *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("%d ", b[i] ); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 4 5 6 7 8 9 10 .. _sp_memcpy_ptr_int_sp_ex_10: .. tab-set:: .. tab-item:: Two Arrays copy : One int at a time with pointers incrementing : Loop * Step 1 : Define two arrays .. code-block:: c int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * Step 2 : Define two pointers .. code-block:: c int *p; int *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 int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; int *p; int *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("%d", b[i] ); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 4 5 6 7 8 9 10 .. _sp_memcpy_ptr_int_sp_ex_11: .. tab-set:: .. tab-item:: Two Arrays copy : memcpy using Array Names * Step 1 : Define two arrays .. code-block:: c int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * 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 int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; memcpy(b, a, sizeof(b)); for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("%d ", b[i] ); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 4 5 6 7 8 9 10 .. _sp_memcpy_ptr_int_sp_ex_12: .. tab-set:: .. tab-item:: Two Arrays copy : memcpy using pointer alias Names * Step 1 : Define two arrays .. code-block:: c int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * Step 2 : Define Pointers .. code-block:: c int *p; int *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 int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; int *p; int *q; p = a; q = b; memcpy(q, p, sizeof(b)); for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("%d ", b[i] ); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 4 5 6 7 8 9 10 .. _sp_memcpy_ptr_int_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 int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * Step 2 : Use first 3 integers in array "b" .. code-block:: c b[0] = 10; b[1] = 20; b[2] = 30; * Step 3 : Use remainig 7 integers in array "b" to copy contents from array "a" .. code-block:: c memcpy(b + 3, a + 3, sizeof(b) - (3 * sizeof(int)) ); * Step 4 : Print the contents of array "b" .. code-block:: c for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("%d", b[i]); } * See full program below .. code-block:: c :linenos: #include #include int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; // Clear garbage contents in array "b" memset(b, 0, sizeof(b)); // Use first 3 integers in array "b" b[0] = 10; b[1] = 20; b[2] = 30; // Use remainig 7 integers in array "b" to copy contents from array "a" memcpy(b + 3, a + 3, sizeof(b) - (3 * sizeof(int)) ); for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("%d", b[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c 10 20 30 4 5 6 7 8 9 10 .. _sp_memcpy_ptr_int_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 int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * Step 2 : Define two pointers .. code-block:: c int *p; int *q; * Step 3 : Extract part of data from array "a" into array "b" .. code-block:: c p = a + 3; q = a + 7; 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("%d", b[i]); } * See full program below .. code-block:: c :linenos: #include #include int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; int *p; int *q; // Clear garbage contents in array "b" memset(b, 0, sizeof(b)); // Extract part of data from array "a" into array "b" p = a + 3; q = a + 7; for (int i = 0; p <= q; i++) { b[i] = *p; p++; } // Print the contents of array "b" for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++) { printf("%d ", b[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c 4 5 6 7 8 0 0 0 0 0 .. _sp_memcpy_ptr_int_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 int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; * Step 2 : Define a Single pointer .. code-block:: c int *p; * Step 3 : Change part of the array using a single pointer .. code-block:: c p = a + 3; p[0] = 100; // p[0] is equal to a[3] p[1] = 200; // p[1] is equal to a[4] p[2] = 300; // p[2] is equal to a[5] * Step 4 : Print the contents of array "a" .. code-block:: c for(int i = 0; i < sizeof(a) / sizeof(a[0]) ; i++) { printf("%d", a[i]); } * See full program below .. code-block:: c :linenos: #include int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *p; // Change part of the array using a single pointer p = a + 3; p[0] = 100; // p[0] is equal to a[3] p[1] = 200; // p[1] is equal to a[4] p[2] = 300; // p[2] is equal to a[5] // Print the contents of array "a" for(int i = 0; i < sizeof(a) / sizeof(a[0]) ; i++) { printf("%d ", a[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 100 200 300 7 8 9 10 .. _sp_memcpy_ptr_int_sp_ex_16: .. tab-set:: .. tab-item:: Single array : Change contents of Single array using memcpy * Step 1 : Define two arrays .. code-block:: c int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10] = {100, 200, 300}; * Step 2 : Change part of array "a" by copying contents from array "b" .. code-block:: c memcpy(a + 3, b, 3 * sizeof(int)); * Step 3 : Print the contents of array "a" .. code-block:: c for(int i = 0; i < sizeof(a) / sizeof(a[0]) ; i++) { printf("%d", a[i]); } * See full program below .. code-block:: c :linenos: #include #include int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10] = {100, 200, 300}; // Change part of array "a" by copying contents from array "b" memcpy(a + 3, b, 3 * sizeof(int)); // Print the contents of array "a" for(int i = 0; i < sizeof(a) / sizeof(a[0]) ; i++) { printf("%d", a[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 100 200 300 7 8 9 10 .. _sp_memcpy_ptr_int_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 int p[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *q; * Step 2 : Allocate heap memory to pointer .. code-block:: c q = malloc(10 * sizeof(int)); * Step 3 : Clear garbage contents of allocated heap memory .. code-block:: c memset(q, 0, 10 * sizeof(int)); * Step 4 : Use standard "memcpy" on heap memory .. code-block:: c memcpy(q, p, 10 * sizeof(int)); * Step 5 : Free heap memory after use .. code-block:: c free(q); * See full program below .. code-block:: c :linenos: #include #include #include int main(void) { int p[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *q; // Allocate 10 bytes memory in heap. Let "q" point to it q = malloc(10 * sizeof(int)); // Clear garbage contents of allocated heap memory memset(q, 0, 10 * sizeof(int)); // Use standard "memcpy" to copy contents into heap memory pointed by "q" memcpy(q, p, 10 * sizeof(int)); // Use "%d" to print contents heap memory pointed by "q" for(int i = 0; i < sizeof(p) / sizeof(p[0]); i++) { printf("%d ", q[i]); } printf("\n"); free(q); } * Output is as below .. code-block:: c 1 2 3 4 5 6 7 8 9 10 .. _sp_memcpy_ptr_int_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 int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * Step 2 : Define two pointers .. code-block:: c int *p; int *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(int *dest, int *src, int size) { memcpy(dest, src, size); } * See full program below .. code-block:: c :linenos: #include #include void my_memcpy_1(int *dest, int *src, int size) { memcpy(dest, src, size); } int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; int *p; int *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("%d ", b[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 4 5 6 7 8 9 10 .. _sp_memcpy_ptr_int_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 int p[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; * Step 2 : Define two pointers .. code-block:: c int *p; int *q; p = a; q = b; * Step 3 : Pass single pointer to a function : Call by Reference .. code-block:: c my_memcpy_2(&q, &p, sizeof(b)); * Step 4 : Define `my_memcpy_2` function .. code-block:: c void my_memcpy_2(int **dest, int **src, int size) { memcpy(*dest, *src, size); } * See full program below .. code-block:: c :linenos: #include #include void my_memcpy_2(int **dest, int **src, int size) { memcpy(*dest, *src, size); } int main(void) { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[10]; int *p; int *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("%d ", b[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c 1 2 3 4 5 6 7 8 9 10 .. 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`