In: Computer Science
Write, specify and prove the function copy that copies a range of values into another array, starting from the first cell of the array. First consider (and specify) that the two ranges are entirely separated. Note: Prototype is as below. void copy(int const* src, int* dst, size_t len){ }
Code for the function:
#include <stdio.h>
void copy(int const *src, int *dst, size_t len)
{
int i;
for (i = 0; i < len; ++i)
{ //iterating len times
*dst = *src; //copyting the valuaq1l of src pointer to dst pointer
++dst; //incrementing destination pointer
++src; //incrementing source pointer
}
}
int main()
{
int arr1[] = {1, 2, 3, 4, 5}; //first array
int arr2[5]; //declaring second array
copy(arr1, arr2, 5); //calling the copy function
int i;
//testing the values in arr2 by printing them
for (i = 0; i < 5; ++i)
{
printf("%d\n", arr2[i]);
}
return 0;
}
Output:
Code Screenshot: