Like Tree1Likes
  • 1 Post By Salem

strange behaviour

This is a discussion on strange behaviour within the C Programming forums, part of the General Programming Boards category; Hi all. The value of the first 2 character members of the structure are the same, so why does the ...

  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
    32,830
    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.
    cfanatic likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 11: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, 07:44 PM
  4. strange behaviour.......
    By surdy in forum C Programming
    Replies: 2
    Last Post: 05-01-2004, 12:50 PM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 01:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21