Memcpy structure single pointer

  • In this section, you are going to learn

How to do memcpy with structure single pointer ?

  • Step 1 : Define two structure variables

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

b = a;
  • Step 3 : Print structures

printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z );
  • See full program below

 1#include <stdio.h>
 2
 3struct ABC {
 4        int x;
 5        int y;
 6        int z;
 7};
 8
 9int main(void)
10{
11        struct ABC a = { .x = 10, .y = 20, .z = 30 };
12        struct ABC b;
13
14        b = a;
15
16        printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z );
17
18        return 0;
19}
  • Output is as below

a.x = 10, a.y = 20, a.z = 30
  • Step 1 : Define two structure variables

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

memcpy(&b, &a, sizeof(struct ABC));
  • Step 3 : Print structures

printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z );
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4struct ABC {
 5        int x;
 6        int y;
 7        int z;
 8};
 9
10int main(void)
11{
12        struct ABC a = { .x = 10, .y = 20, .z = 30 };
13
14        struct ABC b;
15
16        memcpy(&b, &a, sizeof(struct ABC));
17
18        printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z );
19
20        return 0;
21}
  • Output is as below

a.x = 10, a.y = 20, a.z = 30
  • Step 1 : Define two structure variables

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

struct ABC *p;
struct ABC *q;

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

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

printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z );
  • See full program below

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

a.x = 10, a.y = 20, a.z = 30
  • Step 1 : Define two structure variables

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

struct ABC *p;
struct ABC *q;

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

memcpy(q, p, sizeof(struct ABC));
  • Step 4 : Print structures

printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z );
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4struct ABC {
 5        int x;
 6        int y;
 7        int z;
 8};
 9
10int main(void)
11{
12        struct ABC a = { .x = 10, .y = 20, .z = 30 };
13
14        struct ABC b;
15
16        struct ABC *p;
17
18        struct ABC *q;
19
20        p = &a;
21        q = &b;
22
23        memcpy(q, p, sizeof(struct ABC));
24
25        printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z );
26
27        return 0;
28}
  • Output is as below

a.x = 10, a.y = 20, a.z = 30
  • Step 1 : Define two structure variables

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

struct ABC *p;
struct ABC *q;

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

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

printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z );
  • See full program below

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

a.x = 10, a.y = 20, a.z = 30
  • Step 1 : Define two structure variables

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

struct ABC *p;
struct ABC *q;

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

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

printf("a.x = %d, a.y = %d, a.z = %d\n", a.x, a.y, a.z );
  • See full program below

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

a.x = 10, a.y = 20, a.z = 30
  • Step 1 : Define two arrays

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

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("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

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

b[0].x = 1  b[0].y = 2  b[0].z = 3
b[1].x = 10 b[1].y = 20 b[1].z = 30
  • Step 1 : Define two arrays

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

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("*(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

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

*(b + 0).x = 1  *(b + 0).y = 2  *(b + 0).z = 3
*(b + 1).x = 10 *(b + 1).y = 20 *(b + 1).z = 30
  • Step 1 : Define two arrays

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

struct ABC *p;
struct ABC *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
 3struct ABC {
 4        int x;
 5        int y;
 6        int z;
 7};
 8
 9int main(void)
10{
11        struct ABC a[2] = {
12                { .x = 1, .y = 2, .z = 3},
13                { .x = 10, .y = 20, .z = 30},
14        };
15
16        struct ABC b[2];
17
18        struct ABC *p;
19        struct ABC *q;
20
21        p = a;
22        q = b;
23
24        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
25        {
26                q[i] = p[i];
27        }
28
29        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
30        {
31                printf("b[%d].x = %d ", i, b[i].x );
32                printf("b[%d].y = %d ", i, b[i].y );
33                printf("b[%d].z = %d ", i, b[i].z );
34                printf("\n");
35        }
36
37        printf("\n");
38
39        return 0;
40}
  • Output is as below

b[0].x = 1  b[0].y = 2  b[0].z = 3
b[1].x = 10 b[1].y = 20 b[1].z = 30
  • Step 1 : Define two arrays

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

struct ABC *p;
struct ABC *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
 3struct ABC {
 4        int x;
 5        int y;
 6        int z;
 7};
 8
 9int main(void)
10{
11        struct ABC a[2] = {
12                { .x = 1, .y = 2, .z = 3},
13                { .x = 10, .y = 20, .z = 30},
14        };
15
16        struct ABC b[2];
17
18        struct ABC *p;
19        struct ABC *q;
20
21        p = a;
22        q = b;
23
24        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
25        {
26                *q++ = *p++;
27        }
28
29        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
30        {
31                printf("b[%d].x = %d ", i, b[i].x );
32                printf("b[%d].y = %d ", i, b[i].y );
33                printf("b[%d].z = %d ", i, b[i].z );
34                printf("\n");
35        }
36
37        printf("\n");
38
39        return 0;
40}
  • Output is as below

b[0].x = 1  b[0].y = 2  b[0].z = 3
b[1].x = 10 b[1].y = 20 b[1].z = 30
  • Step 1 : Define two arrays

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

memcpy(b, a, sizeof(b));
  • See full program below

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

b[0].x = 1  b[0].y = 2  b[0].z = 3
b[1].x = 10 b[1].y = 20 b[1].z = 30
  • Step 1 : Define two arrays

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

struct ABC *p;
struct ABC *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
 4struct ABC {
 5        int x;
 6        int y;
 7        int z;
 8};
 9
10int main(void)
11{
12        struct ABC a[2] = {
13                { .x = 1, .y = 2, .z = 3},
14                { .x = 10, .y = 20, .z = 30},
15        };
16
17        struct ABC b[2];
18
19        struct ABC *p;
20        struct ABC *q;
21
22        p = a;
23        q = b;
24
25        memcpy(q, p, sizeof(b));
26
27        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
28        {
29                printf("b[%d].x = %d ", i, b[i].x );
30                printf("b[%d].y = %d ", i, b[i].y );
31                printf("b[%d].z = %d ", i, b[i].z );
32                printf("\n");
33        }
34
35        printf("\n");
36
37        return 0;
38 }
  • Output is as below

b[0].x = 1  b[0].y = 2  b[0].z = 3
b[1].x = 10 b[1].y = 20 b[1].z = 30
  • Step 1 : Define two arrays

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”

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”

memcpy(b + 2, a + 2, sizeof(b) - (2 * sizeof(struct ABC)) );
  • Step 4 : Print the contents of array “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");
}
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4struct ABC {
 5        int x;
 6        int y;
 7        int z;
 8};
 9
10int main(void)
11{
12        struct ABC a[4] = {
13                { .x = 1, .y = 2, .z = 3},
14                { .x = 10, .y = 20, .z = 30},
15                { .x = 100, .y = 200, .z = 300},
16                { .x = 1000, .y = 2000, .z = 3000},
17        };
18
19        struct ABC b[4];
20
21        // Clear garbage contents in array "b"
22        memset(b, 0, sizeof(b));
23
24        b[0].x = 11; b[0].y = 22; b[0].z = 33;
25        b[1].x = 44; b[1].y = 55; b[1].z = 66;
26
27        memcpy(b + 2, a + 2, sizeof(b) - (2 * sizeof(struct ABC)) );
28
29        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
30        {
31                printf("b[%d].x = %d ", i, b[i].x );
32                printf("b[%d].y = %d ", i, b[i].y );
33                printf("b[%d].z = %d ", i, b[i].z );
34                printf("\n");
35        }
36
37        printf("\n");
38
39        return 0;
40}
  • Output is as below

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
  • Step 1 : Define two arrays

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

struct ABC *p;
struct ABC *q;
  • Step 3 : 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++;
}
  • Step 4 : Print the contents of array “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");
}
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4struct ABC {
 5        int x;
 6        int y;
 7        int z;
 8};
 9
10int main(void)
11{
12        struct ABC a[4] = {
13                { .x = 1, .y = 2, .z = 3},
14                { .x = 10, .y = 20, .z = 30},
15                { .x = 100, .y = 200, .z = 300},
16                { .x = 1000, .y = 2000, .z = 3000},
17        };
18
19        struct ABC b[4];
20
21        struct ABC *p;
22        struct ABC *q;
23
24        // Clear garbage contents in array "b"
25        memset(b, 0, sizeof(b));
26
27        // Extract part of data from array "a" into array "b"
28        p = a + 1;
29        q = a + 2;
30
31        for (int i = 0; p <= q; i++)
32        {
33                b[i] = *p;
34                p++;
35        }
36
37        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
38        {
39                printf("b[%d].x = %d ", i, b[i].x );
40                printf("b[%d].y = %d ", i, b[i].y );
41                printf("b[%d].z = %d ", i, b[i].z );
42                printf("\n");
43        }
44
45        printf("\n");
46
47        return 0;
48}
  • Output is as below

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
  • Step 1 : Define a Single Dimension array

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

struct ABC *p;
  • Step 3 : 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;
  • Step 4 : 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");
}
  • See full program below

 1#include <stdio.h>
 2
 3struct ABC {
 4        int x;
 5        int y;
 6        int z;
 7};
 8
 9int main(void)
10{
11        struct ABC a[4] = {
12                { .x = 1, .y = 2, .z = 3},
13                { .x = 10, .y = 20, .z = 30},
14                { .x = 100, .y = 200, .z = 300},
15                { .x = 1000, .y = 2000, .z = 3000},
16        };
17
18        struct ABC *p;
19
20        // Change part of the array using a single pointer
21        p = a + 2;
22
23        // p[0] is equal to a[2]
24        p[0].x = 11;
25        p[0].y = 22;
26        p[0].z = 33;
27
28        // p[1] is equal to a[3]
29        p[1].x = 44;
30        p[1].y = 55;
31        p[1].z = 66;
32
33        for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
34        {
35                printf("a[%d].x = %d ", i, a[i].x );
36                printf("a[%d].y = %d ", i, a[i].y );
37                printf("a[%d].z = %d ", i, a[i].z );
38                printf("\n");
39        }
40
41        printf("\n");
42
43        return 0;
44}
  • Output is as below

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
  • Step 1 : Define two arrays

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”

memcpy(a + 1, b + 1, 3 * sizeof(struct ABC));
  • Step 3 : 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");
}
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4struct ABC {
 5        int x;
 6        int y;
 7        int z;
 8};
 9
10
11int main(void)
12{
13        struct ABC a[4] = {
14                { .x = 1, .y = 2, .z = 3},
15                { .x = 10, .y = 20, .z = 30},
16                { .x = 100, .y = 200, .z = 300},
17                { .x = 1000, .y = 2000, .z = 3000},
18        };
19
20        struct ABC b[4] = {
21                { .x = 1, .y = 2, .z = 3},
22                { .x = 11, .y = 22, .z = 33},
23                { .x = 44, .y = 55, .z = 66},
24                { .x = 77, .y = 88, .z = 99},
25        };
26
27        // Change part of array "a" by copying contents from array "b"
28        memcpy(a + 1, b + 1, 3 * sizeof(struct ABC));
29
30        // Print the contents of array "a"
31        for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
32        {
33                printf("a[%d].x = %d ", i, a[i].x );
34                printf("a[%d].y = %d ", i, a[i].y );
35                printf("a[%d].z = %d ", i, a[i].z );
36                printf("\n");
37        }
38
39        printf("\n");
40
41        return 0;
42}
  • Output is as below

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
  • Step 1 : Define an array and a pointer

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

q = malloc(4 * sizeof(struct ABC));
  • Step 3 : Clear garbage contents of allocated heap memory

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

memcpy(q, a, 4 * sizeof(struct ABC));
  • 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
 5struct ABC {
 6        int x;
 7        int y;
 8        int z;
 9};
10
11int main(void)
12{
13        struct ABC a[4] = {
14                { .x = 1, .y = 2, .z = 3},
15                { .x = 10, .y = 20, .z = 30},
16                { .x = 100, .y = 200, .z = 300},
17                { .x = 1000, .y = 2000, .z = 3000},
18        };
19
20        struct ABC *q;
21
22        // Allocate 10 bytes memory in heap. Let "q" point to it
23        q = malloc(4 * sizeof(struct ABC));
24
25        // Clear garbage contents of allocated heap memory
26        memset(q, 0, 4 * sizeof(struct ABC));
27
28        // Use standard "memcpy" to copy contents into heap memory pointed by "q"
29        memcpy(q, a, 4 * sizeof(struct ABC));
30
31        // Use "%d" to print contents heap memory pointed by "q"
32        for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
33        {
34                printf("q[%d].x = %d ", i, q[i].x );
35                printf("q[%d].y = %d ", i, q[i].y );
36                printf("q[%d].z = %d ", i, q[i].z );
37                printf("\n");
38        }
39
40        printf("\n");
41
42        free(q);
43}
  • Output is as below

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
  • Step 1 : Define two arrays

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

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

 1#include <stdio.h>
 2#include <string.h>
 3
 4struct ABC {
 5        int x;
 6        int y;
 7        int z;
 8};
 9
10void my_memcpy_1(struct ABC *dest, struct ABC *src, int size)
11{
12        memcpy(dest, src, size);
13}
14
15int main(void)
16{
17        struct ABC a[4] = {
18                { .x = 1, .y = 2, .z = 3},
19                { .x = 10, .y = 20, .z = 30},
20                { .x = 100, .y = 200, .z = 300},
21                { .x = 1000, .y = 2000, .z = 3000},
22        };
23
24        struct ABC b[4];
25
26        struct ABC *p;
27        struct ABC *q;
28
29        p = a;
30        q = b;
31
32        memset(b, 0, sizeof(b));
33
34        my_memcpy_1(q, p, sizeof(b));
35
36        // Use "%d" to print contents heap memory pointed by "q"
37        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
38        {
39                printf("b[%d].x = %d ", i, b[i].x );
40                printf("b[%d].y = %d ", i, b[i].y );
41                printf("b[%d].z = %d ", i, b[i].z );
42                printf("\n");
43        }
44
45        printf("\n");
46
47        return 0;
48}
  • Output is as below

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
  • Step 1 : Define two arrays

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

struct ABC *p;
struct ABC *q;

p = a;
q = b;
  • Step 3 : Pass single pointer to a function : Call by Value

my_memcpy_2(q, p, sizeof(b));
  • Step 4 : Define my_memcpy_1 function

void my_memcpy_2(struct ABC **dest, struct ABC **src, int size)
{
        memcpy(*dest, *src, size);
}
  • See full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4struct ABC {
 5        int x;
 6        int y;
 7        int z;
 8};
 9
10void my_memcpy_2(struct ABC **dest, struct ABC **src, int size)
11{
12        memcpy(*dest, *src, size);
13}
14
15int main(void)
16{
17        struct ABC a[4] = {
18                { .x = 1, .y = 2, .z = 3},
19                { .x = 10, .y = 20, .z = 30},
20                { .x = 100, .y = 200, .z = 300},
21                { .x = 1000, .y = 2000, .z = 3000},
22        };
23
24        struct ABC b[4];
25
26        struct ABC *p;
27        struct ABC *q;
28
29        p = a;
30        q = b;
31
32        memset(b, 0, sizeof(b));
33
34        my_memcpy_2(&q, &p, sizeof(b));
35
36        // Use "%d" to print contents heap memory pointed by "q"
37        for (int i = 0; i < sizeof(b) / sizeof(b[0]); i++)
38        {
39                printf("b[%d].x = %d ", i, b[i].x );
40                printf("b[%d].y = %d ", i, b[i].y );
41                printf("b[%d].z = %d ", i, b[i].z );
42                printf("\n");
43        }
44
45        printf("\n");
46
47        return 0;
48}
  • Output is as below

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