Skip to content

Commit

Permalink
fix(codes/c): Fix unknown behavior when size is 0 or arr is a null po…
Browse files Browse the repository at this point in the history
…inter (krahets#288)

Co-authored-by: Yudong Jin <[email protected]>
  • Loading branch information
Gonglja and krahets authored Jan 20, 2023
1 parent 31eefe0 commit 0b5761e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions codes/c/include/print_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ extern "C" {
*/
static void printArray(int arr[], int size) {
printf("[");
for (int i = 0; i < size - 1; i++) {
if (arr[i] != NIL) {
printf("%d, ", arr[i]);
} else {
printf("NULL, ");
}
}
if (arr[size - 1] != NIL) {
printf("%d]\n", arr[size - 1]);
if (arr != NULL && size != 0) {
for (int i = 0; i < size - 1; i++) {
if (arr[i] != NIL) {
printf("%d, ", arr[i]);
} else {
printf("NULL, ");
}
}
if (arr[size - 1] != NIL) {
printf("%d]\n", arr[size - 1]);
} else {
printf("NULL]\n");
}
} else {
printf("NULL]\n");
printf("]");
}
}

Expand Down

0 comments on commit 0b5761e

Please sign in to comment.