-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise02.c
68 lines (55 loc) · 2.05 KB
/
exercise02.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// C Primer Plus
// Chapter 10 Exercise 2:
// Write a program that initializes an array-of-double and then copies the
// contents of the array into three other arrays. (All four arrays should be
// declared in the main program.) To make the first copy, use a function with
// array notation. To make the second copy, use a function with pointer
// notation and pointer incrementing. Have the first two functions take as
// arguments the name of the target array, the name of the source array, and
// the number of elements to be copied. Have the third function take as
// arguments the name of the target, the name of the source, and a pointer to
// the element following the last element of the source.
#include <stdio.h>
#define LENGTH 5
// prototype declarations
void copy_arr(double *target, double *source, int arr_len); // copy with array notation
void copy_ptr(double *target, double *source, int arr_len); // copy with pointer notation
void copy_ptrs(double *target, double *source_start, double *source_end); // copy with two pointers
int main(void)
{
double source[LENGTH] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[LENGTH];
double target2[LENGTH];
double target3[LENGTH];
// copy arrays
copy_arr(target1, source, LENGTH);
copy_ptr(target2, source, LENGTH);
copy_ptrs(target3, source, source + LENGTH);
// print array contents
printf("%15s|%15s|%15s\n", "target1", "target2", "target3");
for (int i = 0; i < LENGTH; i++)
printf("%15.3f|%15.3f|%15.3f\n", target1[i], target2[i], target3[i]);
return 0;
}
void copy_arr(double *target, double *source, int arr_len)
{
// copy array using array notation
for (int i = 0; i < arr_len; i++)
{
target[i] = source[i];
}
}
void copy_ptr(double *target, double *source, int arr_len)
{
// copy array using pointer notation
for (int i = 0; i < arr_len; i++)
{
*(target + i) = *(source + i);
}
}
void copy_ptrs(double *target, double *source_start, double *source_end)
{
// copy arr using pointer notation and pointer endpoint
for (double *ptr = source_start; ptr < source_end; ptr++, target++)
*target = *ptr;
}