Memcpy char 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 character single pointer ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Single character copy ` * :ref:`Single character copy using memcpy ` * :ref:`Single character copy using pointers ` * :ref:`Single character copy using pointers with memcpy ` * :ref:`Single character copy using pointers indexing and loop ` * :ref:`Single character copy using pointers increment and loop ` * :ref:`Two Arrays copy : One char at a time with array indexing : Loop ` * :ref:`Two Arrays copy : One char at a time with array indexing (pointer notation): Loop ` * :ref:`Two Arrays copy : One char at a time with pointers indexing : Loop ` * :ref:`Two Arrays copy : One char 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_char_sp_ex_1: .. tab-set:: .. tab-item:: Single character copy * Step 1 : Define two character variables .. code-block:: c char a = 5; char b; * Step 2 : Do character copy .. code-block:: c b = a; * Step 3 : Print characters .. 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) { char a = 5; char 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_char_sp_ex_2: .. tab-set:: .. tab-item:: Single character copy using `memcpy` * Step 1 : Define two character variables .. code-block:: c char a = 5; char b; * Step 2 : Do character copy .. code-block:: c memcpy(&b, &a, sizeof(char)); * Step 3 : Print characters .. 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) { char a = 5; char b; memcpy(&b, &a, sizeof(char)); printf("a = %d, b = %d\n", a, b); return 0; } * Output is as below .. code-block:: c a = 5, b = 5 .. _sp_memcpy_ptr_char_sp_ex_3: .. tab-set:: .. tab-item:: Single character copy using pointers * Step 1 : Define two character variables .. code-block:: c char a = 5; char b; * Step 2 : Define two pointers .. code-block:: c char *p; char *q; p = &a; q = &b; * Step 3 : Do character copy .. code-block:: c *q = *p; * Step 4 : Print characters .. 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) { char a = 5; char b; char *p; char *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_char_sp_ex_4: .. tab-set:: .. tab-item:: Single character copy using pointers with memcpy * Step 1 : Define two character variables .. code-block:: c char a = 5; char b; * Step 2 : Define two pointers .. code-block:: c char *p; char *q; p = &a; q = &b; * Step 3 : Do character copy using memcpy .. code-block:: c memcpy(q, p, sizeof(char)); * Step 4 : Print characters .. 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) { char a = 5; char b; char *p; char *q; p = &a; q = &b; memcpy(q, p, sizeof(char)); printf("a = %d, b = %d\n", a, b); return 0; } * Output is as below .. code-block:: c a = 5, b = 5 .. _sp_memcpy_ptr_char_sp_ex_5: .. tab-set:: .. tab-item:: Single character copy using pointers indexing and loop * Step 1 : Define two character variables .. code-block:: c char a = 5; char b; * Step 2 : Define two pointers .. code-block:: c char *p; char *q; p = &a; q = &b; * Step 3 : Do character copy using memcpy .. code-block:: c for (int i = 0; i < sizeof(char); i++) { q[i] = p[i]; } * Step 4 : Print characters .. 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) { char a = 5; char b; char *p; char *q; p = &a; q = &b; for (int i = 0; i < sizeof(char); i++) { 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_char_sp_ex_6: .. tab-set:: .. tab-item:: Single character copy using pointers increment and loop * Step 1 : Define two character variables .. code-block:: c char a = 5; char b; * Step 2 : Define two pointers .. code-block:: c char *p; char *q; p = &a; q = &b; * Step 3 : Do character copy using memcpy .. code-block:: c for (int i = 0; i < sizeof(char); i++) { *q++ = *p++; } * Step 4 : Print characters .. 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) { char a = 5; char b; char *p; char *q; p = &a; q = &b; for (int i = 0; i < sizeof(char); i++) { *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_char_sp_ex_7: .. tab-set:: .. tab-item:: Two Arrays copy : One char at a time with array indexing : Loop * Step 1 : Define two arrays .. code-block:: c char a[10] = "Laptop123"; char b[10]; * Step 2 : Do copy .. code-block:: c for (int i = 0; i < sizeof(b); i++) { b[i] = a[i]; } * Step 3 : Print destination array .. code-block:: c for (int i = 0; i < sizeof(b); i++) { printf("%c", b[i]); } * See full program below .. code-block:: c :linenos: :emphasize-lines: 10, 15 #include int main(void) { char a[10] = "Laptop123"; char b[10]; for (int i = 0; i < sizeof(b); i++) { b[i] = a[i]; } for (int i = 0; i < sizeof(b); i++) { printf("%c", b[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c Laptop123 .. _sp_memcpy_ptr_char_sp_ex_8: .. tab-set:: .. tab-item:: Two Arrays copy : One char at a time with array indexing (pointer notation): Loop * Step 1 : Define two arrays .. code-block:: c char a[10] = "Laptop123"; char b[10]; * Step 2 : Do copy .. code-block:: c for (int i = 0; i < sizeof(b); i++) { *(b + i) = *(a + i); } * Step 3 : Print destination array .. code-block:: c for (int i = 0; i < sizeof(b); i++) { printf("%c", *(b + i) ); } * See full program below .. code-block:: c :linenos: :emphasize-lines: 10, 15 #include int main(void) { char a[10] = "Laptop123"; char b[10]; for (int i = 0; i < sizeof(b); i++) { *(b + i) = *(a + i); } for (int i = 0; i < sizeof(b); i++) { printf("%c", *(b + i) ); } printf("\n"); return 0; } * Output is as below .. code-block:: c Laptop123 .. _sp_memcpy_ptr_char_sp_ex_9: .. tab-set:: .. tab-item:: Two Arrays copy : One char at a time with pointers indexing : Loop * Step 1 : Define two arrays .. code-block:: c char a[10] = "Laptop123"; char b[10]; * Step 2 : Define two pointers .. code-block:: c char *p; char *q; p = a; q = b; * Step 3 : Do copy .. code-block:: c for (int i = 0; i < sizeof(b); i++) { q[i] = p[i]; } * See full program below .. code-block:: c :linenos: :emphasize-lines: 16 #include int main(void) { char a[10] = "Laptop123"; char b[10]; char *p; char *q; p = a; q = b; for (int i = 0; i < sizeof(b); i++) { q[i] = p[i]; } for (int i = 0; i < sizeof(b); i++) { printf("%c", b[i] ); } printf("\n"); return 0; } * Output is as below .. code-block:: c Laptop123 .. _sp_memcpy_ptr_char_sp_ex_10: .. tab-set:: .. tab-item:: Two Arrays copy : One char at a time with pointers incrementing : Loop * Step 1 : Define two arrays .. code-block:: c char a[10] = "Laptop123"; char b[10]; * Step 2 : Define two pointers .. code-block:: c char *p; char *q; p = a; q = b; * Step 3 : Do copy .. code-block:: c for (int i = 0; i < sizeof(b); i++) { *q++ = *p++; } * See full program below .. code-block:: c :linenos: :emphasize-lines: 16 #include int main(void) { char a[10] = "Laptop123"; char b[10]; char *p; char *q; p = a; q = b; for (int i = 0; i < sizeof(b); i++) { *q++ = *p++; } for (int i = 0; i < sizeof(b); i++) { printf("%c", b[i] ); } printf("\n"); return 0; } * Output is as below .. code-block:: c Laptop123 .. _sp_memcpy_ptr_char_sp_ex_11: .. tab-set:: .. tab-item:: Two Arrays copy : memcpy using Array Names * Step 1 : Define two arrays .. code-block:: c char a[10] = "Laptop123"; char 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) { char a[10] = "Laptop123"; char b[10]; memcpy(b, a, sizeof(b)); for (int i = 0; i < sizeof(b); i++) { printf("%c", b[i] ); } printf("\n"); return 0; } * Output is as below .. code-block:: c Laptop123 .. _sp_memcpy_ptr_char_sp_ex_12: .. tab-set:: .. tab-item:: Two Arrays copy : memcpy using pointer alias Names * Step 1 : Define two arrays .. code-block:: c char a[10] = "Laptop123"; char b[10]; * Step 2 : Define Pointers .. code-block:: c char *p; char *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) { char a[10] = "Laptop123"; char b[10]; char *p; char *q; p = a; q = b; memcpy(q, p, sizeof(b)); for (int i = 0; i < sizeof(b); i++) { printf("%c", b[i] ); } printf("\n"); return 0; } * Output is as below .. code-block:: c Laptop123 .. _sp_memcpy_ptr_char_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 char a[10] = "Laptop123"; char b[10]; * Step 2 : Use first 3 characters in array "b" .. code-block:: c memcpy(b,"Pen", sizeof("Pen")); * Step 3 : Use remainig 7 characters in array "b" to copy contents from array "a" .. code-block:: c memcpy(b + 3, a + 3, sizeof(b) - 3); * Step 4 : Print the contents of array "b" .. code-block:: c for (int i = 0; i < sizeof(b); i++) { printf("%c", b[i]); } * See full program below .. code-block:: c :linenos: #include #include int main(void) { char a[10] = "Laptop123"; char b[10]; // Clear garbage contents in array "b" memset(b, 0, sizeof(b)); // Use first 3 characters in array "b" memcpy(b,"Pen", sizeof("Pen")); // Use remainig 7 characters in array "b" to copy contents from array "a" memcpy(b + 3, a + 3, sizeof(b) - 3); for (int i = 0; i < sizeof(b); i++) { printf("%c", b[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c Pentop123 .. _sp_memcpy_ptr_char_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 char a[10] = "Laptop123"; char b[10]; * Step 2 : Define two pointers .. code-block:: c char *p; char *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); i++) { printf("%c", b[i]); } * See full program below .. code-block:: c :linenos: #include #include int main(void) { char a[10] = "Laptop123"; char b[10]; char *p; char *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); i++) { printf("%c", b[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c top12 .. _sp_memcpy_ptr_char_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 char a[10] = "Laptop123"; * Step 2 : Define a Single pointer .. code-block:: c char *p; * Step 3 : Change part of the array using a single pointer .. code-block:: c p = a + 3; p[0] = 'g'; // p[0] is equal to a[3] p[1] = 'a'; // p[1] is equal to a[4] p[2] = 'p'; // 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) ; i++) { printf("%c", a[i]); } * See full program below .. code-block:: c :linenos: #include int main(void) { char a[10] = "Laptop123"; char *p; // Change part of the array using a single pointer p = a + 3; p[0] = 'g'; // p[0] is equal to a[3] p[1] = 'a'; // p[1] is equal to a[4] p[2] = 'p'; // p[2] is equal to a[5] // Print the contents of array "a" for(int i = 0; i < sizeof(a) ; i++) { printf("%c", a[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c Lapgap123 .. _sp_memcpy_ptr_char_sp_ex_16: .. tab-set:: .. tab-item:: Single array : Change contents of Single array using memcpy * Step 1 : Define two arrays .. code-block:: c char a[10] = "Laptop123"; char b[10] = "gap"; * Step 2 : Change part of array "a" by copying contents from array "b" .. code-block:: c memcpy(a + 3, b, 3); * Step 3 : Print the contents of array "a" .. code-block:: c for(int i = 0; i < sizeof(a) ; i++) { printf("%c", a[i]); } * See full program below .. code-block:: c :linenos: #include #include int main(void) { char a[10] = "Laptop123"; char b[10] = "gap"; // Change part of array "a" by copying contents from array "b" memcpy(a + 3, b, 3); // Print the contents of array "a" for(int i = 0; i < sizeof(a) ; i++) { printf("%c", a[i]); } printf("\n"); return 0; } * Output is as below .. code-block:: c Lapgap123 .. _sp_memcpy_ptr_char_sp_ex_17: .. tab-set:: .. tab-item:: Use of standard strcpy on heap memory * Step 1 : Define two pointers .. code-block:: c char *p; char *q; * Step 2 : Allocate heap memory to two pointers .. code-block:: c p = malloc(10); q = malloc(10); * Step 3 : Clear garbage contents of allocated heap memory .. code-block:: c memset(p, 0, 10); memset(q, 0, 10); * Step 4 : Use standard "strcpy" on heap memory .. code-block:: c strcpy(p, "Laptop123"); strcpy(q, p); * Step 5 : Use "%s" to print contents heap memory pointed by "q" .. code-block:: c printf("q = %s\n", q); * Step 6 : Free heap memory after use .. code-block:: c free(p); free(q); * See full program below .. code-block:: c :linenos: #include #include #include int main(void) { char *p; char *q; // Allocate 10 bytes memory in heap. Let "p" point to it p = malloc(10); // Allocate 10 bytes memory in heap. Let "q" point to it q = malloc(10); // Clear garbage contents of allocated heap memory memset(p, 0, 10); memset(q, 0, 10); // Use standard "strcpy" to copy contents into heap memory pointed by "p" strcpy(p, "Laptop123"); // Use standard "strcpy" to copy contents from heap memory pointed by "p" into heap memory pointed by "q" strcpy(q, p); // Use "%s" to print contents heap memory pointed by "q" printf("q = %s\n", q); free(p); free(q); } * Output is as below .. code-block:: c q = Laptop123 .. _sp_memcpy_ptr_char_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 char a[10] = "Laptop123"; char b[10]; * Step 2 : Define two pointers .. code-block:: c char *p; char *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(char *dest, char *src, int size) { memcpy(dest, src, size); } * See full program below .. code-block:: c :linenos: #include #include void my_memcpy_1(char *dest, char *src, int size) { memcpy(dest, src, size); } int main(void) { char a[10] = "Laptop123"; char b[10]; char *p; char *q; p = a; q = b; memset(b, 0, sizeof(b)); my_memcpy_1(q, p, sizeof(b)); printf("b = %s\n", b); return 0; } * Output is as below .. code-block:: c b = Laptop123 .. _sp_memcpy_ptr_char_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 char a[10] = "Laptop123"; char b[10]; * Step 2 : Define two pointers .. code-block:: c char *p; char *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(char **dest, char **src, int size) { memcpy(*dest, *src, size); } * See full program below .. code-block:: c :linenos: #include #include void my_memcpy_2(char **dest, char **src, int size) { memcpy(*dest, *src, size); } int main(void) { char a[10] = "Laptop123"; char b[10]; char *p; char *q; p = a; q = b; memset(b, 0, sizeof(b)); my_memcpy_2(&q, &p, sizeof(b)); printf("b = %s\n", b); return 0; } * Output is as below .. code-block:: c b = Laptop123 .. 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`