Memcpy int single pointer

  • In this section, you are going to learn

How to do memcpy with integer single pointer ?

  • Step 1 : Define two integer variables

int a = 5;
int b;
  • Step 2 : Do integer copy

b = a;
  • Step 3 : Print integers

printf("a = %d, b = %d\n", a, b);
  • See full program below

 1#include <stdio.h>
 2
 3int main(void)
 4{
 5        int a = 5;
 6        int 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 integer variables

int a = 5;
int b;
  • Step 2 : Do integer copy

memcpy(&b, &a, sizeof(int));
  • Step 3 : Print integers

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        int a = 5;
 7        int b;
 8
 9        memcpy(&b, &a, sizeof(int));
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 integer variables

int a = 5;
int b;
  • Step 2 : Define two pointers

int *p;
int *q;

p = &a;
q = &b;
  • Step 3 : Do integer copy

*q = *p;
  • Step 4 : Print integers

printf("a = %d, b = %d\n", a, b);
  • See full program below

 1#include <stdio.h>
 2
 3int main(void)
 4{
 5        int a = 5;
 6        int b;
 7
 8        int *p;
 9        int *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 integer variables

int a = 5;
int b;
  • Step 2 : Define two pointers

int *p;
int *q;

p = &a;
q = &b;
  • Step 3 : Do integer copy using memcpy

memcpy(q, p, sizeof(int));
  • Step 4 : Print integers

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        int a = 5;
 7        int b;
 8
 9        int *p;
10        int *q;
11
12        p = &a;
13        q = &b;
14
15        memcpy(q, p, sizeof(int));
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 integer variables

int a = 5;
int b;
  • Step 2 : Define two pointers

int *p;
int *q;

p = &a;
q = &b;
  • Step 3 : Do integer copy using memcpy

for (int i = 0; i < sizeof(int); i += 4)
{
        q[i] = p[i];
}
  • Step 4 : Print integers

printf("a = %d, b = %d\n", a, b);
  • See full program below

 1#include <stdio.h>
 2
 3int main(void)
 4{
 5        int a = 5;
 6        int b;
 7
 8        int *p;
 9        int *q;
10
11        p = &a;
12        q = &b;
13
14        for (int i = 0; i < sizeof(int); i += 4)
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 integer variables

int a = 5;
int b;
  • Step 2 : Define two pointers

int *p;
int *q;

p = &a;
q = &b;
  • Step 3 : Do integer copy using memcpy

for (int i = 0; i < sizeof(int); i += 4)
{
         *q++ = *p++;
}
  • Step 4 : Print integers

printf("a = %d, b = %d\n", a, b);
  • See full program below

 1#include <stdio.h>
 2
 3int main(void)
 4{
 5        int a = 5;
 6        int b;
 7
 8        int *p;
 9        int *q;
10
11        p = &a;
12        q = &b;
13
14        for (int i = 0; i < sizeof(int); i += 4)
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

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[10];
  • Step 2 : Do copy

for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
{
        b[i] = a[i];
}
  • Step 3 : Print destination array

for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
{
        printf("%d ", b[i]);
}
  • See full program below

 1#include <stdio.h>
 2
 3int main(void)
 4{
 5        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 6        int b[10];
 7
 8        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
 9        {
10                b[i] = a[i];
11        }
12
13        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
14        {
15                printf("%d ", b[i]);
16        }
17
18        printf("\n");
19
20        return 0;
21}
  • Output is as below

1 2 3 4 5 6 7 8 9 10
  • Step 1 : Define two arrays

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[10];
  • Step 2 : Do copy

for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
{
        *(b + i) = *(a + i);
}
  • Step 3 : Print destination array

for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
{
        printf("%d ", *(b + i) );
}
  • See full program below

 1#include <stdio.h>
 2
 3int main(void)
 4{
 5        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 6        int b[10];
 7
 8        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
 9        {
10                *(b + i) = *(a + i);
11        }
12
13        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
14        {
15                printf("%d ", *(b + i) );
16        }
17
18        printf("\n");
19
20        return 0;
21}
  • Output is as below

1 2 3 4 5 6 7 8 9 10
  • Step 1 : Define two arrays

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[10];
  • Step 2 : Define two pointers

int *p;
int *q;

p = a;
q = b;
  • Step 3 : Do copy

for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
{
        q[i] = p[i];
}
  • See full program below

 1#include <stdio.h>
 2
 3int main(void)
 4{
 5        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 6        int b[10];
 7
 8        int *p;
 9        int *q;
10
11        p = a;
12        q = b;
13
14        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
15        {
16                q[i] = p[i];
17        }
18
19        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
20        {
21                printf("%d ", b[i] );
22        }
23
24        printf("\n");
25
26        return 0;
27}
  • Output is as below

1 2 3 4 5 6 7 8 9 10
  • Step 1 : Define two arrays

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[10];
  • Step 2 : Define two pointers

int *p;
int *q;

p = a;
q = b;
  • Step 3 : Do copy

for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
{
        *q++ = *p++;
}
  • See full program below

 1#include <stdio.h>
 2
 3int main(void)
 4{
 5        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 6        int b[10];
 7
 8        int *p;
 9        int *q;
10
11        p = a;
12        q = b;
13
14        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
15        {
16                *q++ = *p++;
17        }
18
19        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
20        {
21                printf("%d", b[i] );
22        }
23
24        printf("\n");
25
26        return 0;
27}
  • Output is as below

1 2 3 4 5 6 7 8 9 10
  • Step 1 : Define two arrays

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int 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        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 7        int b[10];
 8
 9        memcpy(b, a, sizeof(b));
10
11        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
12        {
13                printf("%d ", b[i] );
14        }
15
16        printf("\n");
17
18        return 0;
19}
  • Output is as below

1 2 3 4 5 6 7 8 9 10
  • Step 1 : Define two arrays

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[10];
  • Step 2 : Define Pointers

int *p;
int *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        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 7        int b[10];
 8
 9        int *p;
10        int *q;
11
12        p = a;
13        q = b;
14
15        memcpy(q, p, sizeof(b));
16
17        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
18        {
19                printf("%d ", b[i] );
20        }
21
22        printf("\n");
23
24        return 0;
25 }
  • Output is as below

1 2 3 4 5 6 7 8 9 10
  • Step 1 : Define two arrays

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”

b[0] = 10;
b[1] = 20;
b[2] = 30;
  • Step 3 : Use remainig 7 integers in array “b” to copy contents from array “a”

memcpy(b + 3, a + 3, sizeof(b) - (3 * sizeof(int)) );
  • Step 4 : Print the contents of array “b”

for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
{
        printf("%d", b[i]);
}
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4int main(void)
 5{
 6        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 7        int b[10];
 8
 9        // Clear garbage contents in array "b"
10        memset(b, 0, sizeof(b));
11
12        // Use first 3 integers in array "b"
13        b[0] = 10;
14        b[1] = 20;
15        b[2] = 30;
16
17        // Use remainig 7 integers in array "b" to copy contents from array "a"
18        memcpy(b + 3, a + 3, sizeof(b) - (3 * sizeof(int)) );
19
20        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
21        {
22                printf("%d", b[i]);
23        }
24
25        printf("\n");
26
27        return 0;
28}
  • Output is as below

10 20 30 4 5 6 7 8 9 10
  • Step 1 : Define two arrays

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[10];
  • Step 2 : Define two pointers

int *p;
int *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) / sizeof(b[0]); i++)
{
        printf("%d", b[i]);
}
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4int main(void)
 5{
 6        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 7        int b[10];
 8
 9        int *p;
10        int *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) / sizeof(b[0]); i++)
27        {
28                printf("%d ", b[i]);
29        }
30
31        printf("\n");
32
33        return 0;
34}
  • Output is as below

4 5 6 7 8 0 0 0 0 0
  • Step 1 : Define a Single Dimension array

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  • Step 2 : Define a Single pointer

int *p;
  • Step 3 : 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]
  • Step 4 : Print the contents of array “a”

for(int i = 0; i < sizeof(a) / sizeof(a[0]) ; i++)
{
        printf("%d", a[i]);
}
  • See full program below

 1#include <stdio.h>
 2
 3int main(void)
 4{
 5        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 6
 7        int *p;
 8
 9        // Change part of the array using a single pointer
10        p = a + 3;
11
12        p[0] = 100; // p[0] is equal to a[3]
13        p[1] = 200; // p[1] is equal to a[4]
14        p[2] = 300; // p[2] is equal to a[5]
15
16        // Print the contents of array "a"
17        for(int i = 0; i < sizeof(a) / sizeof(a[0]) ; i++)
18        {
19                printf("%d ", a[i]);
20        }
21
22        printf("\n");
23
24        return 0;
25}
  • Output is as below

1 2 3 100 200 300 7 8 9 10
  • Step 1 : Define two arrays

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”

memcpy(a + 3, b, 3 * sizeof(int));
  • Step 3 : Print the contents of array “a”

for(int i = 0; i < sizeof(a) / sizeof(a[0]) ; i++)
{
        printf("%d", a[i]);
}
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4int main(void)
 5{
 6        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 7
 8        int b[10] = {100, 200, 300};
 9
10        // Change part of array "a" by copying contents from array "b"
11        memcpy(a + 3, b, 3 * sizeof(int));
12
13        // Print the contents of array "a"
14        for(int i = 0; i < sizeof(a) / sizeof(a[0]) ; i++)
15        {
16                printf("%d", a[i]);
17        }
18
19        printf("\n");
20
21        return 0;
22}
  • Output is as below

1 2 3 100 200 300 7 8 9 10
  • Step 1 : Define an array and a pointer

int p[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int *q;
  • Step 2 : Allocate heap memory to pointer

q = malloc(10 * sizeof(int));
  • Step 3 : Clear garbage contents of allocated heap memory

memset(q, 0, 10 * sizeof(int));
  • Step 4 : Use standard “memcpy” on heap memory

memcpy(q, p, 10 * sizeof(int));
  • Step 5 : Free heap memory after use

free(q);
  • See full program below

 1#include <stdio.h>
 2#include <stdlib.h>
 3#include <string.h>
 4
 5int main(void)
 6{
 7        int p[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 8        int *q;
 9
10        // Allocate 10 bytes memory in heap. Let "q" point to it
11        q = malloc(10 * sizeof(int));
12
13        // Clear garbage contents of allocated heap memory
14        memset(q, 0, 10 * sizeof(int));
15
16        // Use standard "memcpy" to copy contents into heap memory pointed by "q"
17        memcpy(q, p, 10 * sizeof(int));
18
19        // Use "%d" to print contents heap memory pointed by "q"
20        for(int i = 0; i < sizeof(p) / sizeof(p[0]); i++)
21        {
22                printf("%d ", q[i]);
23        }
24
25        printf("\n");
26
27        free(q);
28}
  • Output is as below

1 2 3 4 5 6 7 8 9 10
  • Step 1 : Define two arrays

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int b[10];
  • Step 2 : Define two pointers

int *p;
int *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(int *dest, int *src, int size)
{
        memcpy(dest, src, size);
}
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4void my_memcpy_1(int *dest, int *src, int size)
 5{
 6        memcpy(dest, src, size);
 7}
 8
 9int main(void)
10{
11        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
12
13        int b[10];
14
15        int *p;
16
17        int *q;
18
19        p = a;
20        q = b;
21
22        memset(b, 0, sizeof(b));
23
24        my_memcpy_1(q, p, sizeof(b));
25
26        // Use "%d" to print contents heap memory pointed by "q"
27        for(int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
28        {
29                printf("%d ", b[i]);
30        }
31
32        printf("\n");
33
34        return 0;
35}
  • Output is as below

1 2 3 4 5 6 7 8 9 10
  • Step 1 : Define two arrays

int p[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int b[10];
  • Step 2 : Define two pointers

int *p;
int *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(int **dest, int **src, int size)
{
        memcpy(*dest, *src, size);
}
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4void my_memcpy_2(int **dest, int **src, int size)
 5{
 6        memcpy(*dest, *src, size);
 7}
 8
 9int main(void)
10{
11        int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
12        int b[10];
13
14        int *p;
15        int *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        // Use "%d" to print contents heap memory pointed by "q"
25        for(int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
26        {
27                printf("%d ", b[i]);
28        }
29
30        printf("\n");
31
32        return 0;
33}
  • Output is as below

1 2 3 4 5 6 7 8 9 10