Hi,
I have a structure with two elements namely name and zip.
I need to sort the zip first and within the zip, I need to sort the name.
My structure is a dynamic structure.
I have created the below program it is able to print the sorted zip. But it is unable to sort the name based on the zip. I know I am missing something. But could not figure out what could be wrong.
I need your help in understanding what I have done wrong.
Please help me.
Thanks in Advance
Chari
---------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct address {
char name[40];
char zip[11];
};
int struct_cmp_by_zip(const void *a, const void *b)
{
struct address **ia = (struct address **)a;
struct address **ib = (struct address **)b;
return strcmp((*ia)->zip, (*ib)->zip);
}
int struct_cmp_by_name(const void *a, const void *b)
{
struct address **ia = (struct address **)a;
struct address **ib = (struct address **)b;
return strcmp((*ia)->name, (*ib)->name);
}
void print_struct_array(struct address **array, size_t len)
{
size_t i;
for(i=0; i<len; i++)
printf("[ name: %s \t zip: %s ]\n", array[i]->name, array[i]->zip);
puts("--");
}
/* sorting structs using qsort() example */
void sort_structs_example(void)
{
int i, count, iContinue, iStart, iEnd, iCount;
char n[100];
size_t structs_len;
struct address **structs = NULL;
iContinue = 1;
count = 0;
printf("Do you want to enter the details: (1 to enter, 0 to quit)");
scanf("%d", &iContinue);
while (iContinue)
{
structs = (struct address **)realloc(structs, (sizeof(struct address *) * (count + 1)));
if (structs == NULL) {printf("unable to allocate memory\n"); return;}
structs[count] = (struct address *)malloc(sizeof(struct address));
if (structs[count] == NULL) {printf("unable to allocate memory\n"); return;}
printf("Enter name:"); scanf("%s", structs[count]->name);
printf("Enter zip:"); scanf("%s", structs[count]->zip);
printf("Do you want to enter the details: (1 to enter, 0 to quit)");
scanf("%d", &iContinue);
count++;
structs_len = (size_t)count;
}
//size_t structs_len = sizeof(structs *) / sizeof(struct address *);
printf("structs_len = %d\n", structs_len);
puts("*** Struct original ...");
/* print original struct array */
print_struct_array(structs, structs_len);
puts("*** Struct sorting (zip)...");
/* sort array using qsort functions */
qsort(structs, structs_len, sizeof(struct address *), struct_cmp_by_zip);
/* print sorted struct array */
print_struct_array(structs, structs_len);
puts("*** Struct sorting (zip/name)...");
iStart = iEnd = iCount = 0;
for(i=0; i<=structs_len; i++)
{
if (i == 0)
{
strcpy(n, structs[i]->zip);
iStart = i;
iCount = 1;
continue;
}
if (i < structs_len)
{
if (!strcmp(n, structs[i]->zip))
{
iCount++;
continue;
}
}
iEnd = iStart + (iCount);
qsort(*structs+iStart, iEnd-iStart, sizeof(struct address *), struct_cmp_by_name);
strcpy(n, structs[i]->zip);
iStart = i;
iCount = 1;
}
/* print sorted struct array */
print_struct_array(structs, structs_len);
for (i = 0; i < structs_len; i++)
if (structs[i] != NULL) {free(structs[i]); structs[i] = NULL;}
if (structs != NULL) {free(structs); structs = NULL;}
}
---------------------------------------------------------------------------------------------------



LinkBack URL
About LinkBacks


