-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmallocs.c
More file actions
52 lines (42 loc) · 971 Bytes
/
Copy pathmallocs.c
File metadata and controls
52 lines (42 loc) · 971 Bytes
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
#include <stdio.h>
#include <stdlib.h>
int *allocated_scalar_list(int size, int multiplier)
{
int *result = malloc(size * sizeof(int));
if (result == NULL)
{
return NULL;
}
for (int i = 0; i < size; i++)
{
result[i] = i * multiplier;
}
return result;
}
int main()
{
int size = 5;
int multiplier = 2;
int expected[5];
expected[0] = 0;
expected[1] = 2;
expected[2] = 4;
expected[3] = 6;
expected[4] = 8;
printf("size: %d\nmultiplier: %d\n", size, multiplier);
int *result = allocated_scalar_list(size, multiplier);
for (int j = 0; j < size; j++)
{
if(result[j] == expected[j])
{
printf("result[%d] '%d' - pass\n", j, result[j]);
} else
{
printf(":( -> uh ohhh not matching....");
}
}
// we need to free as the function created it!
free(result);
printf("done!\n");
return 0;
}