Skip to content

Commit b73e713

Browse files
committed
Add -Wconversion compiler warning
1 parent d43bfda commit b73e713

File tree

16 files changed

+97
-92
lines changed

16 files changed

+97
-92
lines changed

Day01.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define CAP 4096
44

55
static int parse(const char *input, int depths[CAP]) {
6-
size_t n = 0;
6+
int n = 0;
77
int charsRead = 0;
88
int filled = 0;
99

Day03.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static int parse(const char *input, int diags[CAP]) {
2424

2525
input += charsRead;
2626

27-
diags[n++] = strtoimax(diagString, NULL, 2);
27+
diags[n++] = (int)strtoimax(diagString, NULL, 2);
2828

2929
assert(n < CAP);
3030
}
@@ -58,7 +58,7 @@ static int partTwoRating(int n, const int diags[n], Rating rating) {
5858
int filtered[n];
5959
int filteredCount = n;
6060

61-
memcpy(filtered, diags, sizeof(*diags) * n);
61+
memcpy(filtered, diags, sizeof(*diags) * (uint32_t)n);
6262

6363
int bitMask = 1 << (BIT_WIDTH - 1);
6464

Day04.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static void playBingo(Input *input, Bingo *bingo) {
6969
bool firstWin = false;
7070

7171
bool alreadyWonBoards[input->boardsCount];
72-
memset(alreadyWonBoards, false, input->boardsCount * sizeof(bool));
72+
memset(alreadyWonBoards, false, (uint32_t)input->boardsCount * sizeof(bool));
7373

7474
for (int i = 0; i < input->numbersCount; ++i) {
7575
int number = input->numbers[i];

Day05.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static int countOverlaps(int h, int w, const int diagram[h][w]) {
9696

9797
static int partOne(int count, const Line lines[count]) {
9898
Line vhLines[count];
99-
memset(vhLines, 0, sizeof(Line) * count);
99+
memset(vhLines, 0, sizeof(Line) * (uint32_t)count);
100100

101101
int countVH = 0;
102102

@@ -109,7 +109,7 @@ static int partOne(int count, const Line lines[count]) {
109109
DiagramSize size = diagramSizeFromLines(countVH, vhLines);
110110

111111
int diagram[size.h][size.w];
112-
memset(diagram, 0, size.w * size.h * sizeof(int));
112+
memset(diagram, 0, (uint32_t)size.w * (uint32_t)size.h * sizeof(int));
113113

114114
drawLines(countVH, vhLines, size.h, size.w, diagram);
115115

@@ -120,7 +120,7 @@ static int partTwo(int count, const Line lines[count]) {
120120
DiagramSize size = diagramSizeFromLines(count, lines);
121121

122122
int diagram[size.h][size.w];
123-
memset(diagram, 0, size.w * size.h * sizeof(int));
123+
memset(diagram, 0, (uint32_t)size.w * (uint32_t)size.h * sizeof(int));
124124

125125
drawLines(count, lines, size.h, size.w, diagram);
126126

Day07.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ static int compareInt(const void *a, const void *b) {
3030

3131
static int partOne(int n, const int xs[n]) {
3232
int sortedXs[n];
33-
memcpy(sortedXs, xs, n * sizeof(xs[0]));
33+
memcpy(sortedXs, xs, (uint32_t)n * sizeof(xs[0]));
3434

35-
qsort(sortedXs, n, sizeof(xs[0]), compareInt); // O(n * log n)
35+
qsort(sortedXs, (uint32_t)n, sizeof(xs[0]), compareInt); // O(n * log n)
3636

3737
int fuels[n];
38-
memset(fuels, 0, n * sizeof(fuels[0]));
38+
memset(fuels, 0, (uint32_t)n * sizeof(fuels[0]));
3939

4040
int midpoint = sortedXs[n / 2]; // Midpoint is the median position
4141
int fuel = 0;

Day09.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static Dim parse(const char *input, uint8_t heightmap[CAP][CAP]) {
2323
int height = c - 48;
2424
assert(height >= 0 && height <= 9);
2525

26-
heightmap[dim.h][x++] = height;
26+
heightmap[dim.h][x++] = (uint8_t)height;
2727
assert(x < CAP);
2828
}
2929

Day10.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void validateSyntax(const char *line, SyntaxResult *result) {
7575
--result->nOpen;
7676
break;
7777
} else {
78-
result->position = line - lineBegin;
78+
result->position = (int)(line - lineBegin);
7979
return;
8080
}
8181
}
@@ -124,7 +124,7 @@ static int64_t partTwo(int n, const char lines[n][SYNTAX_CAP]) {
124124
}
125125
}
126126

127-
qsort(scores, nScores, sizeof(scores[0]), compareInt64); // O(n * log n)
127+
qsort(scores, (uint32_t)nScores, sizeof(scores[0]), compareInt64); // O(n * log n)
128128

129129
return scores[nScores / 2];
130130
}

Day12.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ static CaveId idFromString(const char *s) {
1818
switch (strnlen(s, 6)) {
1919
case 5: return START_CAVE_ID;
2020
case 3: return END_CAVE_ID;
21-
case 2: return (s[0] << 8) | s[1];
22-
case 1: return (s[0] << 8) | s[0];
21+
case 2: return (CaveId)((s[0] << 8) | s[1]);
22+
case 1: return (CaveId)((s[0] << 8) | s[0]);
2323
default: assert(false);
2424
}
2525
}

Day14.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static int parse(const char *input, uint8_t template[TEMPLATE_CAP], uint8_t rule
2727
static int64_t pairFromTemplate(int n, const uint8_t template[n], const uint8_t rules[26][26], int steps) {
2828
// Allocate stack memory for all possible pair combinations for all steps.
2929
int64_t pairsByStep[steps + 1][26][26];
30-
memset(pairsByStep, 0, sizeof(int64_t) * 26 * 26 * (steps + 1));
30+
memset(pairsByStep, 0, sizeof(int64_t) * 26 * 26 * (uint32_t)(steps + 1));
3131

3232
// Insert initial pairs from template into step 0.
3333
for (int i = 0; i < n - 1; ++i) {

Day15.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static int parseSize(const char *input) {
8787
++inputFirstLine;
8888
}
8989

90-
return inputFirstLine - input;
90+
return (int)(inputFirstLine - input);
9191
}
9292

9393
static void parseMap(const char *input, int n, uint8_t map[n][n]) {
@@ -100,7 +100,7 @@ static void parseMap(const char *input, int n, uint8_t map[n][n]) {
100100
input += charsRead;
101101

102102
for (int x = 0; x < n; ++x) {
103-
map[y][x] = row[x] - '0';
103+
map[y][x] = (uint8_t)(row[x] - '0');
104104
}
105105
}
106106
}
@@ -114,17 +114,17 @@ static uint32_t aStarSearch(int n, const uint8_t map[n][n]) {
114114

115115
// Cost of the shortest path from start to node n currently known.
116116
uint32_t gScore[n][n];
117-
memset(gScore, UINT32_MAX, n * n * sizeof(gScore[0][0])); // Fill with "infinity".
117+
memset(gScore, INT32_MAX, (uint32_t)n * (uint32_t)n * sizeof(gScore[0][0])); // Fill with "infinity".
118118

119119
// Current best guess to how short a path from start to goal can be if it goes through node n.
120120
uint32_t fScore[n][n];
121-
memset(fScore, UINT32_MAX, n * n * sizeof(fScore[0][0])); // Fill with "infinity".
121+
memset(fScore, INT32_MAX, (uint32_t)n * (uint32_t)n * sizeof(fScore[0][0])); // Fill with "infinity".
122122

123123
// Set initial score to 0 at (0, 0)
124124
gScore[0][0] = 0;
125125

126126
// Best case scenario from (0, 0) to (n - 1, n - 1).
127-
fScore[0][0] = (n - 1 - 0) + (n - 1 - 0); // h((0,0))
127+
fScore[0][0] = (uint32_t)((n - 1 - 0) + (n - 1 - 0)); // h((0,0))
128128

129129
// Insert start node.
130130
PrioQueue_enqueue(fScore[0][0], (0 << 16) | 0, &nodeByLowestPrio);
@@ -158,21 +158,21 @@ static uint32_t aStarSearch(int n, const uint8_t map[n][n]) {
158158
if (tentativeGScore < gScore[ny][nx]) {
159159
// Found a cheaper path than recorded, record this one.
160160
gScore[ny][nx] = tentativeGScore;
161-
fScore[ny][nx] = tentativeGScore + ((n - 1 - ny) + (n - 1 - nx));
161+
fScore[ny][nx] = tentativeGScore + (uint32_t)((n - 1 - ny) + (n - 1 - nx));
162162

163163
// Insert neighbor node. O(log n).
164-
PrioQueue_enqueue(fScore[ny][nx], (ny << 16) | nx, &nodeByLowestPrio);
164+
PrioQueue_enqueue(fScore[ny][nx], (uint32_t)(ny << 16) | nx, &nodeByLowestPrio);
165165
}
166166
}
167167
}
168168
}
169169
}
170170

171-
static int partOne(int n, const uint8_t map[n][n]) {
171+
static uint32_t partOne(int n, const uint8_t map[n][n]) {
172172
return aStarSearch(n, map);
173173
}
174174

175-
static int partTwo(int n, const uint8_t tile[n][n]) {
175+
static uint32_t partTwo(int n, const uint8_t tile[n][n]) {
176176
int m = n * 5;
177177
uint8_t map[m][m];
178178

0 commit comments

Comments
 (0)