Thread: strange behaviour

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    strange behaviour

    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;
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're using strcmp() on things which do NOT have a \0 at the end of them.

    Try
    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;
    }
    It doesn't sort, but it doesn't crash either.

    Perhaps single step the code in a debugger and see what your break statement is for.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Thanks Salem!
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange behaviour
    By cfanatic in forum C Programming
    Replies: 9
    Last Post: 07-13-2012, 10:41 PM
  2. Some very strange behaviour with argv
    By dnl in forum C Programming
    Replies: 6
    Last Post: 03-09-2010, 01:53 PM
  3. Strange behaviour of GCC
    By BlackOps in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 06:44 PM
  4. strange behaviour.......
    By surdy in forum C Programming
    Replies: 2
    Last Post: 05-01-2004, 11:50 AM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM