fix(codes/c): Fix unknown behavior when size is 0 or arr is a null pointer (#288)

Co-authored-by: Yudong Jin <krahets@163.com>
pull/292/head
Zero 2 years ago committed by GitHub
parent 31eefe028a
commit 0b5761eaf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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("]");
}
}

Loading…
Cancel
Save