Broad Network


Copying an Array in the C Computer Language

Part 28 of the Complete C Course

Foreword: Copying an Array in the C Computer Language

By: Chrysanthus Date Published: 22 Jun 2024

Introduction

For one array to be copied to another, the sizes (lengths) of the two arrays have to be the same, and the element types have to be the same. Consider the following arrays:

    float arr1[] = {0.0, 1.1, 2.2, 3.3, 4.4};
    float arr2[5];

The sizes of both arrays are the same, and the element types are the same. Remember that each float element occupies 4 bytes in memory. This means that 0.0 occupies 4 bytes in memory. After copying, all the elements of the destination array are replaced with all the elements of the source array. Arr2 here, has 4-byte memory locations already allocated, though it does not yet have practical float values.

An array can be copied in two ways: using the for-loop or using the memcpy() function of the  C string library. Read and test the following program that copies arr1 to arr2, using the for-loop:

    #include <stdio.h>
    
    float arr1[] = {0.0, 1.1, 2.2, 3.3, 4.4};
    float arr2[5];
    
    int main(int argc, char *argv[])
    {
        for (int i=0; i<5; i++)
            arr2[i] = arr1[i];
            
        for (int i=0; i<5; i++)
            printf("%f ", arr2[i]);
        printf("\n");

        return 0;
    }

The output is: 0.000000 1.100000 2.200000 3.300000 4.400000 . The two arrays have been declared outside the main() function. The first for-loop in the main() function copies the elements of the array one-by-one. Note the use and positions of the variable i, to hold the different indexes. Since there is only one statement for the body of this for-loop, there is no need for the curly brackets (in other words, the braces become optional when the body consists of only one statement). The second for-loop prints out the values of the destination array. The last printf() statement is not part of the body of the second for-loop. It just prints an empty new line, sending the cursor at the output, to the beginning of the next line.

Notice that the for-loops are inside a function, the main() function. A loop, be it do-while loop, while-loop or for-loop cannot be outside a function in C. Declarations can be outside a function, as in the case for the above two arrays. Definition including initialization, cannot be  outside a function, unless it is part of the declaration. However, after declaration without initialization of an identifier outside functions, the initialization of the identifier can be inside a function, but not outside functions. Also, function call cannot be outside a function.

The memcpy Function
The memcpy() predefined function is in the string library. Each library in C, has a header file. The header file has the interface (summary) to the library. In order to use the predefined memcpy() function of the string library, the header file, string.h of the string library, has to be included into the program. The whole library does not have to be included into the program.

The memcpy() predefined function copies bytes and not the elements, from the source array to the destination array. Remember that the element locations in an array are just next to one another. So, if all that array portion in memory can be copied and dump into another portion in memory, that should be alright. The synopsis of the predefined memcpy function is:

    #include <string.h>
        void * memcpy(void * restrict s2, const void * restrict s1, size_t n);

that is, n bytes pointed to by s1, are copied to the memory area, beginning from the memory byte location, pointed to by s2. So copying an array by memcpy predefined function instead of using a for-loop, has more risks. In the following program, the memcpy predefined function replaces the for-loop above:

    #include <stdio.h>
    #include <string.h>
    
    float arr1[] = {0.0, 1.1, 2.2, 3.3, 4.4};
    float arr2[5];

    int main(int argc, char *argv[])
    {
        memcpy(arr2, arr1, 20);

        for (int i=0; i<5; i++)
            printf("%f ", arr2[i]);
        printf("\n");

        return 0;
    }

The output is: 0.000000 1.100000 2.200000 3.300000 4.400000; same as before. Notice the inclusion of the header file, "string.h" at the top of the program. The first argument of the memcpy function is arr2 without the square brackets. The second argument is arr1 without the square brackets. The third argument is the integer, 20. Each float number occupies 4 bytes in memory. There are 5 float numbers in the array. So the array to be copied from, consists of 4 x 5 = 20 consecutive bytes, in memory. An easier way to obtain the number of bytes, is to use the sizeof() operator as in the following statement:

        memcpy(arr2, arr1, sizeof(arr1));

The argument for the sizeof operator is the name of the array, without the square brackets. The sizeof operator returns the total number of bytes in the object (array).



Related Links

More Related Links

Cousins

BACK NEXT

Comments