forked from cjleggett/week4section
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_practice.c
More file actions
88 lines (73 loc) · 1.94 KB
/
pointer_practice.c
File metadata and controls
88 lines (73 loc) · 1.94 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(int*, int*);
char* concat(char*, char*);
int* append(int*, int, int*);
void printls(int*, int);
int main(void)
{
// Uncomment to test swap
// int a = get_int("Integer 1: ");
// int b = get_int("Integer 2: ");
// swap(&a, &b);
// printf("Integer 1: %i\nInteger 2: %i\n", a, b);
// Uncomment to test concat
// Then make sure that there are no memory leaks, and fix any that you find.
// char *c = get_string("String 1: ");
// char *d = get_string("String 2: ");
// char *cd = concat(c, d);
// printf("%s + %s = %s\n", c, d, cd);
// CHALLENGE: Uncomment to test append
// Then make sure that there are no memory leaks, and fix any that you find.
// Initiallly, the list has length 1:
// int length = 1;
// int *ls = malloc(length * sizeof(int));
// ls[0] = 5;
// while (true)
// {
// int new_int = get_int("Type a non-negative number to add. When you're done, type a negative number: ");
// if (new_int < 0)
// {
// break;
// }
// int *new_ls = append(ls, new_int, &length);
// ls = malloc(length * sizeof(int));
// // copying over the new array
// for (int i = 0; i < length; i ++)
// {
// ls[i] = new_ls[i];
// }
// printls(ls, length);
// }
}
// Swaps the values of two integer pointers
void swap(int *a, int *b)
{
return;
}
// Concatenates two strings
char* concat(char *c, char *d)
{
return "TODO";
}
// Adds an integer to the end of a list of integers
int* append(int *ls, int next, int *current_length)
{
return NULL;
}
// Prints a list of integers
void printls(int* nums, int length)
{
printf("[");
for (int i = 0; i < length; i ++)
{
printf("%i", nums[i]);
if (i != length - 1)
{
printf(", ");
}
}
printf("]\n");
}