Hi all.
The value of the first 2 character members of the structure are the same, so why does the first if condition keep executing? Why doesn't the second if execute?
Thank you.
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
struct Char
{
char ch;
int ctr;
};
char *ptr[5], temp;
int count, compval;
struct Char list[5] = { {'x', 23}, {'x', 4}, {'i', 83}, {'s', 34}, {'a', 12} };
printf("Before sort: ");
for(count = 0; count < 5; count++) // initialise pointers
{
ptr[count] = &list[count].ch;
printf("%c ", *ptr[count]);
}
for(count = 0; count < 4; count++)
{
if((compval = strcmp(ptr[count], ptr[count + 1])) > 0) // ptr[count] > ptr[count + 1]
{
temp = list[count].ch;
list[count].ch = list[count + 1].ch;
list[count + 1].ch = temp;
}
if((compval = strcmp(ptr[count], ptr[count + 1])) == 0) // ptr[count] == ptr[count + 1]
{
puts("values are equal");
break;
}
}
printf("\nAfter sort: ");
for(count = 0; count < 5; count++)
printf("%c ", *ptr[count]);
return 0;
}