I made this program to reverse an array but it only works when I put in an odd number of digits in the array. If I put in even number of digits it doesn't print anything.
For example: If I put {1,2,3,4,5} the program will print {5,4,3,2,1} but if the input is {1,2,3,4} it doesn't print anything.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, *arr, i, count;
count = 0;
scanf("%d", &num);
arr = (int *)malloc(num * sizeof(int));
for (i = 0; i < num; i++) {
scanf("%d", arr + i);
count++;
}
int l_Count, h_Count, temp;
temp = 0;
h_Count = count - 1;
l_Count = 0;
while (l_Count != h_Count) {
temp = *(arr + l_Count);
*(arr + l_Count) = *(arr + h_Count);
*(arr + h_Count) = temp;
h_Count--;
l_Count++;
}
for (i = 0; i < num; i++) {
printf("%d ", *(arr + i));
}
return 0;
}
-fsanitize=addressshould find this easily. Update: yupcountreally used for? A synonym fornum?). Also, consider what can happen when the number of elements is even. Won't an ascending low counter and descending high counter eventually reside next to each other, then pass each other, thereby never tripping the very specificl_Count != h_Countcondition ?while(l_Count != h_Count)-->while(l_Count < h_Count)