Memcpy char single pointer
In this section, you are going to learn
How to do memcpy with character single pointer ?
Topics in this section,
Two Arrays copy : One char at a time with array indexing : Loop
Two Arrays copy : One char at a time with array indexing (pointer notation): Loop
Two Arrays copy : One char at a time with pointers indexing : Loop
Two Arrays copy : One char at a time with pointers incrementing : Loop
Two Arrays copy : memcpy using Array Names : Relative position copy
Single array : Change contents of Single array using a Single pointer
Custom memcpy with single pointer : Pass single pointer to a function : Call by Value
Custom memcpy with single pointer : Pass single pointer to a function : Call by Reference
Step 1 : Define two character variables
char a = 5;
char b;
Step 2 : Do character copy
b = a;
Step 3 : Print characters
printf("a = %d, b = %d\n", a, b);
See full program below
1#include <stdio.h>
2
3int main(void)
4{
5 char a = 5;
6 char b;
7
8 b = a;
9
10 printf("a = %d, b = %d\n", a, b);
11
12 return 0;
13}
Output is as below
a = 5, b = 5
Step 1 : Define two character variables
char a = 5;
char b;
Step 2 : Do character copy
memcpy(&b, &a, sizeof(char));
Step 3 : Print characters
printf("a = %d, b = %d\n", a, b);
See full program below
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
5{
6 char a = 5;
7 char b;
8
9 memcpy(&b, &a, sizeof(char));
10
11 printf("a = %d, b = %d\n", a, b);
12
13 return 0;
14}
Output is as below
a = 5, b = 5
Step 1 : Define two character variables
char a = 5;
char b;
Step 2 : Define two pointers
char *p;
char *q;
p = &a;
q = &b;
Step 3 : Do character copy
*q = *p;
Step 4 : Print characters
printf("a = %d, b = %d\n", a, b);
See full program below
1#include <stdio.h>
2
3int main(void)
4{
5 char a = 5;
6 char b;
7
8 char *p;
9 char *q;
10
11 p = &a;
12 q = &b;
13
14 *q = *p;
15
16 printf("a = %d, b = %d\n", a, b);
17
18 return 0;
19}
Output is as below
a = 5, b = 5
Step 1 : Define two character variables
char a = 5;
char b;
Step 2 : Define two pointers
char *p;
char *q;
p = &a;
q = &b;
Step 3 : Do character copy using memcpy
memcpy(q, p, sizeof(char));
Step 4 : Print characters
printf("a = %d, b = %d\n", a, b);
See full program below
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
5{
6 char a = 5;
7 char b;
8
9 char *p;
10 char *q;
11
12 p = &a;
13 q = &b;
14
15 memcpy(q, p, sizeof(char));
16
17 printf("a = %d, b = %d\n", a, b);
18
19 return 0;
20}
Output is as below
a = 5, b = 5
Step 1 : Define two character variables
char a = 5;
char b;
Step 2 : Define two pointers
char *p;
char *q;
p = &a;
q = &b;
Step 3 : Do character copy using memcpy
for (int i = 0; i < sizeof(char); i++)
{
q[i] = p[i];
}
Step 4 : Print characters
printf("a = %d, b = %d\n", a, b);
See full program below
1#include <stdio.h>
2
3int main(void)
4{
5 char a = 5;
6 char b;
7
8 char *p;
9 char *q;
10
11 p = &a;
12 q = &b;
13
14 for (int i = 0; i < sizeof(char); i++)
15 {
16 q[i] = p[i];
17 }
18
19 printf("a = %d, b = %d\n", a, b);
20
21 return 0;
22}
Output is as below
a = 5, b = 5
Step 1 : Define two character variables
char a = 5;
char b;
Step 2 : Define two pointers
char *p;
char *q;
p = &a;
q = &b;
Step 3 : Do character copy using memcpy
for (int i = 0; i < sizeof(char); i++)
{
*q++ = *p++;
}
Step 4 : Print characters
printf("a = %d, b = %d\n", a, b);
See full program below
1#include <stdio.h>
2
3int main(void)
4{
5 char a = 5;
6 char b;
7
8 char *p;
9 char *q;
10
11 p = &a;
12 q = &b;
13
14 for (int i = 0; i < sizeof(char); i++)
15 {
16 *q++ = *p++;
17 }
18
19 printf("a = %d, b = %d\n", a, b);
20
21 return 0;
22}
Output is as below
a = 5, b = 5
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Do copy
for (int i = 0; i < sizeof(b); i++)
{
b[i] = a[i];
}
Step 3 : Print destination array
for (int i = 0; i < sizeof(b); i++)
{
printf("%c", b[i]);
}
See full program below
1#include <stdio.h>
2
3int main(void)
4{
5 char a[10] = "Laptop123";
6 char b[10];
7
8 for (int i = 0; i < sizeof(b); i++)
9 {
10 b[i] = a[i];
11 }
12
13 for (int i = 0; i < sizeof(b); i++)
14 {
15 printf("%c", b[i]);
16 }
17
18 printf("\n");
19
20 return 0;
21}
Output is as below
Laptop123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Do copy
for (int i = 0; i < sizeof(b); i++)
{
*(b + i) = *(a + i);
}
Step 3 : Print destination array
for (int i = 0; i < sizeof(b); i++)
{
printf("%c", *(b + i) );
}
See full program below
1#include <stdio.h>
2
3int main(void)
4{
5 char a[10] = "Laptop123";
6 char b[10];
7
8 for (int i = 0; i < sizeof(b); i++)
9 {
10 *(b + i) = *(a + i);
11 }
12
13 for (int i = 0; i < sizeof(b); i++)
14 {
15 printf("%c", *(b + i) );
16 }
17
18 printf("\n");
19
20 return 0;
21}
Output is as below
Laptop123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Define two pointers
char *p;
char *q;
p = a;
q = b;
Step 3 : Do copy
for (int i = 0; i < sizeof(b); i++)
{
q[i] = p[i];
}
See full program below
1#include <stdio.h>
2
3int main(void)
4{
5 char a[10] = "Laptop123";
6 char b[10];
7
8 char *p;
9 char *q;
10
11 p = a;
12 q = b;
13
14 for (int i = 0; i < sizeof(b); i++)
15 {
16 q[i] = p[i];
17 }
18
19 for (int i = 0; i < sizeof(b); i++)
20 {
21 printf("%c", b[i] );
22 }
23
24 printf("\n");
25
26 return 0;
27}
Output is as below
Laptop123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Define two pointers
char *p;
char *q;
p = a;
q = b;
Step 3 : Do copy
for (int i = 0; i < sizeof(b); i++)
{
*q++ = *p++;
}
See full program below
1#include <stdio.h>
2
3int main(void)
4{
5 char a[10] = "Laptop123";
6 char b[10];
7
8 char *p;
9 char *q;
10
11 p = a;
12 q = b;
13
14 for (int i = 0; i < sizeof(b); i++)
15 {
16 *q++ = *p++;
17 }
18
19 for (int i = 0; i < sizeof(b); i++)
20 {
21 printf("%c", b[i] );
22 }
23
24 printf("\n");
25
26 return 0;
27}
Output is as below
Laptop123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Do copy
memcpy(b, a, sizeof(b));
See full program below
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
5{
6 char a[10] = "Laptop123";
7 char b[10];
8
9 memcpy(b, a, sizeof(b));
10
11 for (int i = 0; i < sizeof(b); i++)
12 {
13 printf("%c", b[i] );
14 }
15
16 printf("\n");
17
18 return 0;
19}
Output is as below
Laptop123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Define Pointers
char *p;
char *q;
p = a;
q = b;
Step 3 : Do copy
memcpy(q, p, sizeof(b));
See full program below
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
5{
6 char a[10] = "Laptop123";
7 char b[10];
8
9 char *p;
10 char *q;
11
12 p = a;
13 q = b;
14
15 memcpy(q, p, sizeof(b));
16
17 for (int i = 0; i < sizeof(b); i++)
18 {
19 printf("%c", b[i] );
20 }
21
22 printf("\n");
23
24 return 0;
25 }
Output is as below
Laptop123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Use first 3 characters in array “b”
memcpy(b,"Pen", sizeof("Pen"));
Step 3 : Use remainig 7 characters in array “b” to copy contents from array “a”
memcpy(b + 3, a + 3, sizeof(b) - 3);
Step 4 : Print the contents of array “b”
for (int i = 0; i < sizeof(b); i++)
{
printf("%c", b[i]);
}
See full program below
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
5{
6 char a[10] = "Laptop123";
7 char b[10];
8
9 // Clear garbage contents in array "b"
10 memset(b, 0, sizeof(b));
11
12 // Use first 3 characters in array "b"
13 memcpy(b,"Pen", sizeof("Pen"));
14
15 // Use remainig 7 characters in array "b" to copy contents from array "a"
16 memcpy(b + 3, a + 3, sizeof(b) - 3);
17
18 for (int i = 0; i < sizeof(b); i++)
19 {
20 printf("%c", b[i]);
21 }
22
23 printf("\n");
24
25 return 0;
26}
Output is as below
Pentop123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Define two pointers
char *p;
char *q;
Step 3 : 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++;
}
Step 4 : Print the contents of array “b”
for (int i = 0; i < sizeof(b); i++)
{
printf("%c", b[i]);
}
See full program below
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
5{
6 char a[10] = "Laptop123";
7 char b[10];
8
9 char *p;
10 char *q;
11
12 // Clear garbage contents in array "b"
13 memset(b, 0, sizeof(b));
14
15 // Extract part of data from array "a" into array "b"
16 p = a + 3;
17 q = a + 7;
18
19 for (int i = 0; p <= q; i++)
20 {
21 b[i] = *p;
22 p++;
23 }
24
25 // Print the contents of array "b"
26 for (int i = 0; i < sizeof(b); i++)
27 {
28 printf("%c", b[i]);
29 }
30
31 printf("\n");
32
33 return 0;
34}
Output is as below
top12
Step 1 : Define a Single Dimension array
char a[10] = "Laptop123";
Step 2 : Define a Single pointer
char *p;
Step 3 : 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]
Step 4 : Print the contents of array “a”
for(int i = 0; i < sizeof(a) ; i++)
{
printf("%c", a[i]);
}
See full program below
1#include <stdio.h>
2
3int main(void)
4{
5 char a[10] = "Laptop123";
6
7 char *p;
8
9 // Change part of the array using a single pointer
10 p = a + 3;
11
12 p[0] = 'g'; // p[0] is equal to a[3]
13 p[1] = 'a'; // p[1] is equal to a[4]
14 p[2] = 'p'; // p[2] is equal to a[5]
15
16 // Print the contents of array "a"
17 for(int i = 0; i < sizeof(a) ; i++)
18 {
19 printf("%c", a[i]);
20 }
21
22 printf("\n");
23
24 return 0;
25}
Output is as below
Lapgap123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10] = "gap";
Step 2 : Change part of array “a” by copying contents from array “b”
memcpy(a + 3, b, 3);
Step 3 : Print the contents of array “a”
for(int i = 0; i < sizeof(a) ; i++)
{
printf("%c", a[i]);
}
See full program below
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
5{
6 char a[10] = "Laptop123";
7 char b[10] = "gap";
8
9 // Change part of array "a" by copying contents from array "b"
10 memcpy(a + 3, b, 3);
11
12 // Print the contents of array "a"
13 for(int i = 0; i < sizeof(a) ; i++)
14 {
15 printf("%c", a[i]);
16 }
17
18 printf("\n");
19
20 return 0;
21}
Output is as below
Lapgap123
Step 1 : Define two pointers
char *p;
char *q;
Step 2 : Allocate heap memory to two pointers
p = malloc(10);
q = malloc(10);
Step 3 : Clear garbage contents of allocated heap memory
memset(p, 0, 10);
memset(q, 0, 10);
Step 4 : Use standard “strcpy” on heap memory
strcpy(p, "Laptop123");
strcpy(q, p);
Step 5 : Use “%s” to print contents heap memory pointed by “q”
printf("q = %s\n", q);
Step 6 : Free heap memory after use
free(p);
free(q);
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5int main(void)
6{
7 char *p;
8 char *q;
9
10 // Allocate 10 bytes memory in heap. Let "p" point to it
11 p = malloc(10);
12
13 // Allocate 10 bytes memory in heap. Let "q" point to it
14 q = malloc(10);
15
16 // Clear garbage contents of allocated heap memory
17 memset(p, 0, 10);
18 memset(q, 0, 10);
19
20 // Use standard "strcpy" to copy contents into heap memory pointed by "p"
21 strcpy(p, "Laptop123");
22
23 // Use standard "strcpy" to copy contents from heap memory pointed by "p" into heap memory pointed by "q"
24 strcpy(q, p);
25
26 // Use "%s" to print contents heap memory pointed by "q"
27 printf("q = %s\n", q);
28
29 free(p);
30 free(q);
31}
Output is as below
q = Laptop123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Define two pointers
char *p;
char *q;
p = a;
q = b;
Step 3 : Pass single pointer to a function : Call by Value
my_memcpy_1(q, p, sizeof(b));
Step 4 : Define my_memcpy_1 function
void my_memcpy_1(char *dest, char *src, int size)
{
memcpy(dest, src, size);
}
See full program below
1#include <stdio.h>
2#include <string.h>
3
4void my_memcpy_1(char *dest, char *src, int size)
5{
6 memcpy(dest, src, size);
7}
8
9int main(void)
10{
11 char a[10] = "Laptop123";
12 char b[10];
13
14 char *p;
15 char *q;
16
17 p = a;
18 q = b;
19
20 memset(b, 0, sizeof(b));
21
22 my_memcpy_1(q, p, sizeof(b));
23
24 printf("b = %s\n", b);
25
26 return 0;
27}
Output is as below
b = Laptop123
Step 1 : Define two arrays
char a[10] = "Laptop123";
char b[10];
Step 2 : Define two pointers
char *p;
char *q;
p = a;
q = b;
Step 3 : Pass single pointer to a function : Call by Reference
my_memcpy_2(&q, &p, sizeof(b));
Step 4 : Define my_memcpy_2 function
void my_memcpy_2(char **dest, char **src, int size)
{
memcpy(*dest, *src, size);
}
See full program below
1#include <stdio.h>
2#include <string.h>
3
4void my_memcpy_2(char **dest, char **src, int size)
5{
6 memcpy(*dest, *src, size);
7}
8
9int main(void)
10{
11 char a[10] = "Laptop123";
12 char b[10];
13
14 char *p;
15 char *q;
16
17 p = a;
18 q = b;
19
20 memset(b, 0, sizeof(b));
21
22 my_memcpy_2(&q, &p, sizeof(b));
23
24 printf("b = %s\n", b);
25
26 return 0;
27}
Output is as below
b = Laptop123
Current Module
Previous Module
Next Module
Other Modules