Could some one help me out with the basics of Structures ? How to use them in C programs ? etc..

Thanx in advance.

Here is a demo of sorting an array of struct (the array holds a 'Contact list'.)

typedef struct Contacts
{
    char name[16];
    int  area;
    int  phone;
} Contacts;

Here is the program:

/*
    C qsort examples ...
    NOTE: needs a C compiler with qsort and const var's

    demos ... qsort ...
    sorting an array of struct on various fields ... and combined fields
    sorting an array of C strings with (and without) ignoring case
    sorting an array of int's
*/

/* this version 2010-05-16 */

/* http://developers-heaven.net/forum/index.php/topic,46.0.html */


#include <stdio.h>
#include <stdlib.h> /* qsort */
#include <string.h>
#include <ctype.h> /* tolower */

typedef struct Contacts
{
    char name[16]; /* max length of name is 15 char's ... */
    int  area;
    int  phone;
} Contacts;

/* will only handle ints where difference is a valid int ... */
int compareInt( const void* x, const void* y )
{
    return( *(const int*)x - *(const int*)y );
}

int compareStr( const void* x, const void* y )
{
    return strcmp( *(const char**)x, *(const char**)y );
}

int compareStrIgnoreCase( const void* x, const void* y )
{
    const char* s1 = *(const char**)x;
    const char* s2 = *(const char**)y;
    while( *s1 != 0 && *s2 != 0 )
    {
        if( tolower(*s1) != tolower(*s2) )
            break;
        ++s1; ++s2;
    }

    /* now test end conditions .... */
    return tolower(*s1) - tolower(*s2);
}

int compareContactsNameIgnoreCase( const void* x, const void* y )
{
    const Contacts* c1 = (const Contacts*)x;
    const Contacts* c2 = (const Contacts*)y;
    int i = 0;
    while( c1->name[i] != 0 && c2->name[i] != 0 )
    {
        if( tolower(c1->name[i]) != tolower(c2->name[i]) )
            break;
        ++i;
    }
    /* now test end conditions .... */
    if( c1->name[i] == 0  &&   c2->name[i] == 0 )
    {
        if( c1->area == c2->area ) return c1->phone - c2->phone;
        else return c1->area - c2->area;
    }
    /* else ... */
    return tolower(c1->name[i]) - tolower(c2->name[i]);
}

int compareContactsPhone( const void* x, const void* y )
{
    const Contacts* c1 = (const Contacts*)x;
    const Contacts* c2 = (const Contacts*)y;
    if( c1->area == c2->area && c1->phone == c2->phone )
        return strcmp(c1->name, c2->name);
    if( c1->area == c2->area )
        return c1->phone - c2->phone;
    return c1->area - c2->area;
}

void print_book( const Contacts c[], int size );



int main () /* * * * * * * * * * MAIN BEGINS * * * * * * * *  * * * */
{
    /* get some test data into arrays of type struct, C string, int ... */
    Contacts book[] =
    {
        {"joe", 416, 4442388}, {"zam", 519, 1234567}, {"anne", 705, 1234455},
        {"Tam", 905, 5555555}, {"Fran", 416, 7778900}, {"Anne", 705, 1116789 }
    };

    char* aryStr[] = { "joe", "zam", "anne", "Tam", "Fran", "Anne"  };

    int values[] = { 40, 10, 100, -1, 123, -7, 0, 90, 20, 25 };

    /* qsort an array of type int ... */
    int i,
        len = sizeof values / sizeof values[0];

    qsort( values, len, sizeof(int), compareInt );
    for( i = 0; i < len; ++i )
        printf( "%d ", values[i] );
   putchar( '\n' );

    /* qsort an array of type C string, i.e. an array of pointers to char ... */
    len = sizeof aryStr / sizeof aryStr[0];
    qsort( aryStr, len, sizeof(char*), compareStr );
    for( i = 0; i < len; ++i )
        printf( "%s ", aryStr[i] );
    putchar( '\n' );

    /* or sorted with case ignored ... */
    qsort( aryStr, len, sizeof(char*), compareStrIgnoreCase );
    for( i = 0; i < len; ++i )
        printf( "%s ", aryStr[i] );
    putchar( '\n' );

    /* show unsorted array of struct ... */
    len = (sizeof book / sizeof book[0]);
    print_book( book, len );
    putchar( '\n' );

    /* sort array of struct under name field FIRST ... and ignore case ... */
    qsort( book, len, sizeof(Contacts), compareContactsNameIgnoreCase );
    print_book( book, len );
    putchar( '\n' );

    /* sort array of struct under phone fieldS FIRST ...  */
    qsort( book, len, sizeof(Contacts), compareContactsPhone );
    print_book( book, len );

    printf( "\nPress 'Enter' to continue ... " );
    getchar();
    return 0;

}/* * * * * * * * * * * * * * MAIN ENDS * * * * * * * * * * * * * * */



void print_book( const Contacts c[], int size )
{
    int i;
    for( i = 0; i < size; ++i )
        printf( "%-15s %3d-%7d\n", c[i].name, c[i].area, c[i].phone );
}
This article has been dead for over six months. Start a new discussion instead.