sorting an array of strings using qsort

Discussion in 'C Programming' started by Cstudent, May 10, 2009.

  1. Cstudent

    Cstudent Guest

    Im getting and "EXC_BAD_ACCESS" runtime error with my code. Can anyone
    help me out?

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>



    static int sortstring( const void *str1, const void *str2 );
    typedef int (*funcptr)();

    int main (int argc, const char * argv[])
    {
    int inx = 0;
    funcptr p_sortstring;
    char *starr[] =
    {
    "dog",
    "Arm",
    "Cat",
    "Eric",
    "Bat",
    "Fork",
    "hello",
    "Geroge",
    "Mike",
    "Alabama"
    };

    size_t count = sizeof(starr)/sizeof(*starr);
    qsort( starr, count, sizeof(char*), p_sortstring );
    for (inx = 0; inx < 10; inx++)
    printf("%s ", starr[inx]);




    return 0;
    }

    static int sortstring( const void *str1, const void *str2 )
    {
    const char *rec1 = str1;
    const char *rec2 = str2;
    int val = strcmp(rec1, rec2);


    return val;
    }
     
    Cstudent, May 10, 2009
    #1
    1. Advertising

  2. Cstudent

    luserXtrog Guest

    On May 9, 10:40 pm, Cstudent <> wrote:
    > Im getting and "EXC_BAD_ACCESS" runtime error with my code. Can anyone
    > help me out?
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > static int sortstring( const void *str1, const void *str2 );
    > typedef int (*funcptr)();
    >
    > int main (int argc, const char * argv[])
    > {
    >         int inx = 0;
    >         funcptr p_sortstring;


    A pointer gots to point to something!
    try:
    funcptr p_sortstring = sortstring;
    Or eliminate the variable entirely and just use sortstring
    directly as argument.

    >         char *starr[] =
    >         {
    >                 "dog",
    >                 "Arm",
    >                 "Cat",
    >                 "Eric",
    >                 "Bat",
    >                 "Fork",
    >                 "hello",
    >                 "Geroge",
    >                 "Mike",
    >                 "Alabama"
    >         };
    >
    >         size_t count = sizeof(starr)/sizeof(*starr);
    >         qsort( starr, count, sizeof(char*), p_sortstring );


    p_sortstring wasn't initialized, so qsort receives a
    pointer of indeterminate value.

    >         for (inx = 0; inx < 10; inx++)
    >                 printf("%s ", starr[inx]);
    >
    >         return 0;
    >
    > }
    >
    > static int sortstring( const void *str1, const void *str2 )
    > {
    >         const char *rec1 = str1;
    >         const char *rec2 = str2;
    >         int val = strcmp(rec1, rec2);
    >
    >         return val;
    >
    > }
    >
    >


    --
    lxt
     
    luserXtrog, May 10, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ingo Brueckl
    Replies:
    5
    Views:
    486
    Keith Thompson
    Dec 19, 2003
  2. Dimitris Mandalidis

    qsort mode-based sorting

    Dimitris Mandalidis, May 9, 2004, in forum: C Programming
    Replies:
    6
    Views:
    361
  3. sorting 2d arrays using qsort

    , Jun 11, 2005, in forum: C Programming
    Replies:
    23
    Views:
    1,605
    CBFalconer
    Jun 14, 2005
  4. yogeshmk

    qsort() not sorting -:(

    yogeshmk, Jul 11, 2006, in forum: C Programming
    Replies:
    9
    Views:
    358
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
    Jul 11, 2006
  5. Replies:
    6
    Views:
    742
Loading...

Share This Page