C-Copy-Rules

Array <— Array : Single Dimension : An array can not be copied to another array using = operator

Comments

#include <stdio.h>
#include <string.h>

char carr[5] = {10,20,30,40,50};
int iarr[5] = {100,200,300,400,500};
float farr[5] = {100,200,300,400,500};
double darr[5] = {100,200,300,400,500};

struct ABC {
    int x;
};

struct ABC sarr[2] = {
            { .x = 10},
            { .x = 20},
    };

int main()
{
    char d_carr[5];
    int d_iarr[5];
    float d_farr[5];
    double d_darr[5];
    struct ABC d_sarr[5];

    // Below statements are not valid. Use memcpy instead
    d_carr = carr;
    d_iarr = iarr;
    d_farr = farr;
    d_darr = darr;
    d_sarr = sarr;

    return 0;
}

An array can not be copied to another array using = operator

Array <— Array : Single Dimension : An array can be copied to another array using memcpy

Comments

#include <stdio.h>
#include <string.h>

char carr[5] = {10,20,30,40,50};
int iarr[5] = {100,200,300,400,500};
float farr[5] = {100,200,300,400,500};
double darr[5] = {100,200,300,400,500};

struct ABC {
    int x;
};

struct ABC sarr[2] = {
            { .x = 10 },
            { .x = 20 },
    };

int main()
{
    char d_carr[5];
    int d_iarr[5];
    float d_farr[5];
    double d_darr[5];
    struct ABC d_sarr[5];
    int i;

    printf("*** Dump character array ***\n");
    // Below statements are valid
    memcpy(d_carr, carr, sizeof(d_carr));
    for(i = 0; i < 5; i++)
        printf("%d\n", d_carr[i]);

    printf("*** Dump integer array ***\n");
    memcpy(d_iarr, iarr, sizeof(d_iarr));
    for(i = 0; i < 5; i++)
        printf("%d\n", d_iarr[i]);

    printf("*** Dump float array ***\n");
    memcpy(d_farr, farr, sizeof(d_farr));
    for(i = 0; i < 5; i++)
        printf("%f\n", d_farr[i]);

    printf("*** Dump double array ***\n");
    memcpy(d_darr, darr, sizeof(d_darr));
    for(i = 0; i < 5; i++)
        printf("%lf\n", d_darr[i]);

    printf("*** Dump structure array ***\n");
    memcpy(d_sarr, sarr, sizeof(d_sarr));
    printf("%d %d\n", d_sarr[0].x, d_sarr[1].x);

    return 0;
}

An array can be copied to another array using memcpy

Heap <— Array : Single Dimension : An array of elements can be copied to a memory block in heap using memcpy

Comments

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

char carr[5] = {10,20,30,40,50};
int iarr[5] = {100,200,300,400,500};
float farr[5] = {100,200,300,400,500};
double darr[5] = {100,200,300,400,500};

struct ABC {
    int x;
};

struct ABC sarr[2] = {
            { .x = 10 },
            { .x = 20 },
    };

int main()
{
    char *p_carr;
    int *p_iarr;
    float *p_farr;
    double *p_darr;
    struct ABC *p_sarr;
    int i;

    printf("*** Dump character array ***\n");
    // Below statements are valid
    p_carr = malloc(sizeof(char) * 5);
    memcpy(p_carr, carr, sizeof(carr));
    for(i = 0; i < 5; i++)
        printf("%d\n", p_carr[i]);
        
    printf("*** Dump integer array ***\n");
    p_iarr = malloc(sizeof(int) * 5);
    memcpy(p_iarr, iarr, sizeof(iarr));
    for(i = 0; i < 5; i++)
        printf("%d\n", p_iarr[i]);

    printf("*** Dump float array ***\n");
    p_farr = malloc(sizeof(float) * 5);
    memcpy(p_farr, farr, sizeof(farr));
    for(i = 0; i < 5; i++)
        printf("%f\n", p_farr[i]);

    printf("*** Dump double array ***\n");
    p_darr = malloc(sizeof(double) * 5);
    memcpy(p_darr, darr, sizeof(darr));
    for(i = 0; i < 5; i++)
        printf("%lf\n", p_darr[i]);

    printf("*** Dump structure array ***\n");
    p_sarr = malloc(sizeof(struct ABC) * 2);
    memcpy(p_sarr, sarr, sizeof(sarr));
    printf("%d %d\n", p_sarr[0].x, p_sarr[1].x);

    free(p_carr);
    free(p_iarr);
    free(p_farr);
    free(p_darr);
    free(p_sarr);

    return 0;
}

An array of elements can be copied to a memory block in heap using memcpy

Array <— Heap : Single Dimension : A memory block in heap can be copied to an array using memcpy

Comments

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

char carr[5];
int iarr[5];
float farr[5];
double darr[5];

struct ABC {
    int x;
};

struct ABC sarr[2];

int main()
{
    char *p_carr;
    int *p_iarr;
    float *p_farr;
    double *p_darr;
    struct ABC *p_sarr;
    int i;

    printf("*** Dump character array ***\n");
    // Below statements are valid
    p_carr = malloc(sizeof(char) * 5);
    p_carr[0] = p_carr[1] = p_carr[2] = p_carr[3] = p_carr[4] = 10;
    memcpy(carr, p_carr, sizeof(carr));
    for(i = 0; i < 5; i++)
        printf("%d\n", carr[i]);

    printf("*** Dump integer array ***\n");
    p_iarr = malloc(sizeof(int) * 5);
    p_iarr[0] = p_iarr[1] = p_iarr[2] = p_iarr[3] = p_iarr[4] = 10;
    memcpy(iarr, p_iarr, sizeof(iarr));
    for(i = 0; i < 5; i++)
        printf("%d\n", iarr[i]);

    printf("*** Dump float array ***\n");
    p_farr = malloc(sizeof(float) * 5);
    p_farr[0] = p_farr[1] = p_farr[2] = p_farr[3] = p_farr[4] = 10;
    memcpy(farr, p_farr, sizeof(farr));
    for(i = 0; i < 5; i++)
        printf("%f\n", farr[i]);

    printf("*** Dump double array ***\n");
    p_darr = malloc(sizeof(double) * 5);
    p_darr[0] = p_darr[1] = p_darr[2] = p_darr[3] = p_darr[4] = 10;
    memcpy(darr, p_darr, sizeof(darr));
    for(i = 0; i < 5; i++)
        printf("%lf\n", darr[i]);

    printf("*** Dump structure array ***\n");
    p_sarr = malloc(sizeof(struct ABC) * 2);
    p_sarr[0].x = p_sarr[1].x = 20;
    memcpy(sarr, p_sarr, sizeof(sarr));
    printf("%d %d\n", sarr[0].x, sarr[1].x);

    free(p_carr);
    free(p_iarr);
    free(p_farr);
    free(p_darr);
    free(p_sarr);

    return 0;
}

A memory block in heap can be copied to an array using memcpy

Heap <— Heap : Single Dimension : A memory block in heap can be copied to another block in heap using memcpy

Comments

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

struct ABC {
    int x;
};

int main()
{
    char *carr, *p_carr;
    int *iarr, *p_iarr;
    float *farr, *p_farr;
    double *darr, *p_darr;
    struct ABC *sarr, *p_sarr;
    int i;

    printf("*** Dump character array ***\n");
    // Below statements are valid
    carr = malloc(sizeof(char) * 5);
    p_carr = malloc(sizeof(char) * 5);
    p_carr[0] = p_carr[1] = p_carr[2] = p_carr[3] = p_carr[4] = 10;
    memcpy(carr, p_carr, 5);
    for(i = 0; i < 5; i++)
        printf("%d\n", carr[i]);

    printf("*** Dump integer array ***\n");
    iarr = malloc(sizeof(int) * 5);
    p_iarr = malloc(sizeof(int) * 5);
    p_iarr[0] = p_iarr[1] = p_iarr[2] = p_iarr[3] = p_iarr[4] = 10;
    memcpy(iarr, p_iarr, 20);
    for(i = 0; i < 5; i++)
        printf("%d\n", iarr[i]);

    printf("*** Dump float array ***\n");
    farr = malloc(sizeof(float) * 5);
    p_farr = malloc(sizeof(float) * 5);
    p_farr[0] = p_farr[1] = p_farr[2] = p_farr[3] = p_farr[4] = 10;
    memcpy(farr, p_farr, 20);
    for(i = 0; i < 5; i++)
        printf("%f\n", farr[i]);

    printf("*** Dump double array ***\n");
    darr = malloc(sizeof(double) * 5);
    p_darr = malloc(sizeof(double) * 5);
    p_darr[0] = p_darr[1] = p_darr[2] = p_darr[3] = p_darr[4] = 10;
    memcpy(darr, p_darr, 40);
    for(i = 0; i < 5; i++)
        printf("%lf\n", darr[i]);

    printf("*** Dump structure array ***\n");
    sarr = malloc(sizeof(struct ABC) * 2);
    p_sarr = malloc(sizeof(struct ABC) * 2);
    p_sarr[0].x = p_sarr[1].x = 20;
    memcpy(sarr, p_sarr, 8);
    printf("%d %d\n", sarr[0].x, sarr[1].x);

    free(p_carr);
    free(p_iarr);
    free(p_farr);
    free(p_darr);
    free(p_sarr);

    free(carr);
    free(iarr);
    free(farr);
    free(darr);
    free(sarr);
    return 0;
}

A memory block in heap can be copied to another block in heap using memcpy

Structure <— Structure : A structure instance can be copied directly to another structure instance

Comments

#include <stdio.h>
#include <string.h>

struct XYZ
{
    int a;
    int b;
};

struct ABC
{
    int x;
    int y;
    int arr[5];
    struct XYZ xyz;
};

int main()
{
    struct ABC X = {
                    .x = 10,
                    .y = 20,
                    .arr = {1,2,3,4,5},
                    .xyz = {
                            .a = 100,
                            .b = 200,
                            }
                    };
    struct ABC Y = X;

    // prints 10,20,4
    printf("%d %d %d %d %d\n", Y.x, Y.y, Y.arr[3], Y.xyz.a, Y.xyz.b);

    return 0;
}