Thread: beginner needs help

  1. #1
    Unregistered
    Guest

    beginner needs help

    hi, i'm new to C and need to no can anyone help with a small problem.
    wondering is it possible when im creating, a database system in C that can store information, can i make the information be stored alphabetically, and if so how do i go about it?

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    I suspect qsort() is what you want.
    Jason Deckard

  3. #3
    Unregistered
    Guest
    how would i go about that? im pritty new to C and havent done qsort() before.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This came up on a board search, it's a wonderful thing, technology.
    Code:
    void sortarray ( double array1[], int num ) 
    { 
      qsort ( array1, num, sizeof array1[0], cmp );
    }
    
    int cmp ( const void *x, const void *y )
    {
      if ( *(const double*)x < *(const double*)y )
        return -1;
      if ( *(const double*)x > *(const double*)y )
        return  1;
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    qsort() is found in stdlib.h. Its prototype is:
    Code:
    void qsort( void *base, size_t nel, size_t size, int (*compar)(const void *, const void *) );
    This may look intimidating, but it doesn't have to be. Let's take a look at each argument:
    • base - The memory address of the list of items to sort. If your entries are in an array, simply put the array name here.
      nel - The number of elements in your array.
      size - The size of each element in the array (not the size of the entire array).
      compar - The address of the function used to compare any two elements in your array.
    Assuming you are comparing strings, you may either use strcmp() as your compare function or encapsulate it in your own function:
    Code:
    int compare( void *a, void *b )
    {
      return ( strcmp(a,b) );
    }
    Here's a quick example:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int compare( void *a, void *b )
    {
      return strcmp( a, b );
    }
    
    int main( void )
    {
      int i;
      char array[5][10] = { "One", "Two", "Three", "Four", "Five" };
    
      qsort( array, 5, 10, &compare );
    
      for ( i=0; i<5; i++ )
        printf( "%s\n", array[i] );
    
      return 0;
    }
    The output is:
    • Five
      Four
      One
      Three
      Two
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me with this beginner C++ Program!
    By ClearSights in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 10:22 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  3. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM