I want to decrement a pointer to the last element of an array and check for the condition that if the value of the pointer was smaller than 5, do nothing and go to the next round of loop and if not, that is if the value is equal or bigger than 5, print the value. So, for the array in the example, I want only 6 to be printed, that is the first encounter of a value equal or bigger than 5. I tried the code below but while being compiled with no error, it doesn't print any value. I'd appreciate your comments on this.
#include<stdio.h>
//The is a C program to decrement an array from the last element to the first.
int x[11] = {5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2};
int *lastElem, count;
int main (void) {
lastElem = &x[10];
for (count = 0; count < 11; count++)
if (abs(*lastElem) < 5)
continue;
else
printf("%d\n", *lastElem--);
return 0;
}
There is a problem in your decrement logic. If the value is less than 5, you're missing the decremet.
check the below code.
EDIT:
As you modified your question to include the
absolute value
comparions, the changes are like below.Note : When making some major changes [which will change the behaviour and output] to the original question asked, do make a note of that, and if possible, mention the edit properly.