Like Tree1Likes
  • 1 Post By MK27

Can Anyone??

This is a discussion on Can Anyone?? within the C Programming forums, part of the General Programming Boards category; Can anyone please explain me, step by step, each line of the following sorting algorithm which sorts the input lines ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    Can Anyone??

    Can anyone please explain me, step by step, each line of the following sorting algorithm which sorts the input lines numerically, if -n is given at the command line. (Program taken from the book "The C programming language, by brian kernighan and Dennis Ritchie" - Chapter 5. Section 5.11, page no.119)

    Code:
    #include<stdio.h>
    #include<string.h>
    
    #define MAXLINES 5000;
    char *lineptr[MAXLINES};
    
    int readlines(char *lineptr[], int nlines);
    void writelines(char *lineptr[], int nlines);
    
    void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *);
    int numcmp(char *, char *);
    
    main(int argc, char *argv[])
    {
    int nlines;
    int numeric = 0;
    
    if(argc > 1 && strcmp(argv[1],"-n") == 0)
    numeric = 1;
    if((nlines = readlines(lineptr, MAXLINES))>=0)
    {
    qsort((void **)lineptr, 0, nlines-1,(int (*)(void*, void*))(numeric ? numcmp : strcmp);
    writelines(lineptr, nlines);
    return 0;
    }
    
    else
    {
    printf("Input too short\n");
    return 1;
    }
    }

    what does this line mean..."qsort((void **)lineptr, 0, nlines-1,(int (*)(void*, void*))(numeric ? numcmp : strcmp);"

    .
    .
    .

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by juice View Post
    what does this line mean..."qsort((void **)lineptr, 0, nlines-1,(int (*)(void*, void*))(numeric ? numcmp : strcmp);"
    It's a call to qsort:

    qsort

    But I'm guessing it is from before a point when qsort() was standardized, because the parameters are wrong. The first arg is still the array to sort, but it looks as if the 3rd arg, instead of the 2nd, is the number of items in the array, the 2nd arg is just zero. There is no item size param, implying this qsort works on an array of pointers (which is what lineptr is, and the (void**) cast is also different from standard qsort. The last bit:

    (int (*)(void*, void*))(numeric ? numcmp : strcmp)

    Is a function pointer using the ternery operator ?, such that if "numeric" is non-zero then numcmp() will be used as the comparison function otherwise strcmp() will be used.
    juice likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

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