Sorting an Array of pointers to structures 
Author Message
 Sorting an Array of pointers to structures

Enclosed is my source code. What i would like to do actually is to sort
these structures by thier zip codes in ascending order. Can someone point
give me directions on how to do so. This program takes input from a file
with those values and a blank line between each structure. just need to sort
them by ascending zip code order. Thanks.

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

#define MAXNUM 50

struct address {
 char name[MAXNUM];
 char address[MAXNUM];
 char city_state[MAXNUM];
 int zip;
   char blank[MAXNUM];

Quote:
};

int getinfo (struct address *infoptr);
void putinfo (struct address *infoptr);

int main (void) {

 struct address *infoarry;
   struct address *temparry;

   int c = 0, i=0, j, numstruct=18;

   if ((infoarry = (struct address *) calloc(numstruct,
   sizeof(struct address))) == NULL)
   {
    printf("Unable to allocate memory.\n");
      exit (1);
   }

   if ((temparry = (struct address *) calloc(numstruct,
   sizeof(struct address))) == NULL)
   {
    printf("Unable to allocate memory.\n");
      exit (1);
   }

   while (getinfo (infoarry + c) && ++c < numstruct);

   printf ("\n");
   for (j=0; j < c; j++)
    putinfo(&infoarry[j]);
   free ((void*)infoarry);
  return 0;

Quote:
}

/**********************************************************************/
int getinfo (struct address *infoptr)
{
   char instr[MAXNUM];
 gets (infoptr->name);
   gets (infoptr->address);
 gets (infoptr->city_state);
 infoptr->zip=atoi(gets(instr));
   gets (infoptr->blank);
   return 1;

Quote:
}

/**********************************************************************/
void putinfo(struct address *infoptr)
{
 printf ("%s\n", infoptr->name);
 printf ("%s\n", infoptr->address);
 printf ("%s\n", infoptr->city_state);
 printf ("%d\n", infoptr->zip);
   printf ("%s\n", infoptr->blank);
Quote:
}



Sat, 25 Oct 2003 06:59:14 GMT  
 Sorting an Array of pointers to structures

Quote:

> Enclosed is my source code. What i would like to do actually is to sort
> these structures by thier zip codes in ascending order. Can someone point
> give me directions on how to do so. This program takes input from a file
> with those values and a blank line between each structure. just need to sort
> them by ascending zip code order. Thanks.

Certainly...

        void sort(address *array_to_sort)
        {
                /* sort array - implement this yourself */

                return;
        }

In other words, give it your best shot, post some code, and we'll help
you.  We don't do homework.

--
Duncan Bayne
2000 Honda NSR150RR

-----------------------------------------------------------------------

Kuro5hin: dangermouse            Slashdot: dangermouse_nz (UID 315952)
Phone:    025 626 3023
-----------------------------------------------------------------------
The open source model tends to emphasize design and development.
Testing is boring and expensive.

                    -- Steve Lipner, Microsoft Security Response Centre
-----------------------------------------------------------------------



Sat, 25 Oct 2003 07:48:18 GMT  
 Sorting an Array of pointers to structures

Quote:

> Enclosed is my source code. What i would like to do actually is to sort
> these structures by thier zip codes in ascending order. Can someone point
> give me directions on how to do so. This program takes input from a file
> with those values and a blank line between each structure. just need to
> sort them by ascending zip code order. Thanks.

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

Big tip: Learn C++, not C. Start with /Accelerated C++/. It will show how
to use:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

Quote:
> struct address {
>  char name[MAXNUM];
>  char address[MAXNUM];
>  char city_state[MAXNUM];
>  int zip;

Why is this an int? Will you ever add two zips?

Quote:
>    char blank[MAXNUM];
> };

To sort a vector of those first overload operator <:

bool operator<(address const &a, address const &b)
{
    return a.zip < b.zip;

Quote:
}

Now pack a vector full of addresses - not an array of pointers to
addresses. Use them by value:

std::vector<address> addresses;

Then sort them:

std::sort(addresses.begin(), addresses.end());

We must repeat the admonition to learn C++; it's faster to write simple
code like this in.

--

============== http://phlip.webjump.com ==============
  --  Wanted: Marriage counselor who also keeps pet rats  --



Sat, 25 Oct 2003 07:47:42 GMT  
 Sorting an Array of pointers to structures

Quote:


> > Enclosed is my source code. What i would like to do actually is to sort
> > these structures by thier zip codes in ascending order. Can someone point
> > give me directions on how to do so. This program takes input from a file
> > with those values and a blank line between each structure. just need to
> > sort them by ascending zip code order. Thanks.

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

> Big tip: Learn C++, not C. Start with /Accelerated C++/. It will show how
> to use:

I'll leave that alone, except to point out that your statement looks
an awful lot like a troll from this side of the fence (c.l.c.).

<snip>

Quote:
> We must repeat the admonition to learn C++; it's faster to write simple
> code like this in.

Is it now?  Funny, because in C I could write less code than you did -
one function definition and one function call - no needless copying
into a vector, which could potentially be quite expensive.

To the original poster: look up qsort() in your favorite C book.

Micah

--
The way of a fool is right in his own eyes, but he who
heeds counsel is wise.
                                        Proverbs 12:15



Sat, 25 Oct 2003 08:41:54 GMT  
 Sorting an Array of pointers to structures

Quote:


> > Enclosed is my source code. What i would like to do actually is to sort
> > these structures by thier zip codes in ascending order. Can someone point
> > give me directions on how to do so. This program takes input from a file
> > with those values and a blank line between each structure. just need to
> > sort them by ascending zip code order. Thanks.

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

> Big tip: Learn C++, not C. Start with /Accelerated C++/. It will show how
> to use:

> #include <iostream>
> #include <string>
> #include <algorithm>
> #include <vector>

> > struct address {
> >  char name[MAXNUM];
> >  char address[MAXNUM];
> >  char city_state[MAXNUM];
> >  int zip;

> Why is this an int? Will you ever add two zips?

> >    char blank[MAXNUM];
> > };

> To sort a vector of those first overload operator <:

> bool operator<(address const &a, address const &b)
> {
>     return a.zip < b.zip;
> }

> Now pack a vector full of addresses - not an array of pointers to
> addresses. Use them by value:

> std::vector<address> addresses;

> Then sort them:

> std::sort(addresses.begin(), addresses.end());

> We must repeat the admonition to learn C++; it's faster to write simple
> code like this in.

Why are you posting this to c.l.c?  Follow ups set.

--

http://www.qwikpages.com/backstreets/cbfalconer :=(down for now)
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Sat, 25 Oct 2003 11:16:56 GMT  
 Sorting an Array of pointers to structures
The easiest way to sort an array of structures (or anything else for that
matter) is with the qsort() standard c-library function.  You first write a
small function that returns an integer something like what strcmp() returns.
For example:

int compare(const void *p1, const void *p2)
{
    struct address * addr1 = (struct address *)p1;
    struct address * addr2 = (struct address *)p2;
    return addr->zip - addr2->zip;

Quote:
}

Then at the place where you want your array sorted:

    qsort(infoarry, numstruct, sizeof(struct address), compare);

 -----  Posted via NewsOne.Net: Free (anonymous) Usenet News via the Web  -----
  http://newsone.net/ -- Free reading and anonymous posting to 60,000+ groups
   NewsOne.Net prohibits users from posting spam.  If this or other posts



Sat, 25 Oct 2003 11:24:10 GMT  
 Sorting an Array of pointers to structures

Quote:

> Enclosed is my source code. What i would like to do actually is to sort
> these structures by thier zip codes in ascending order. Can someone point
> give me directions on how to do so. This program takes input from a file
> with those values and a blank line between each structure. just need to sort
> them by ascending zip code order. Thanks.

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

> #define MAXNUM 50

> struct address {
>  char name[MAXNUM];
>  char address[MAXNUM];
>  char city_state[MAXNUM];
>  int zip;
>    char blank[MAXNUM];
> };

> int getinfo (struct address *infoptr);
> void putinfo (struct address *infoptr);

> int main (void) {

>  struct address *infoarry;
>    struct address *temparry;

>    int c = 0, i=0, j, numstruct=18;

>    if ((infoarry = (struct address *) calloc(numstruct,
>    sizeof(struct address))) == NULL)

You are allocating an array of struct address here (with a useless
cast in c). Not an array of pointers to struct address.
Instad you need:

struct address **infoarray;

/* allocate the array of pointers */
infoarray=malloc(numstruct * sizeof *infoarray);

/* allocate the structures */
for(i=0;i<numstruct;i++)
   infoarray[i]=malloc(sizeof *infoarray[0]);

Where I left the checking for failure of malloc out.

Or

struct address *array;
struct address **infoarray;

/* allocate an array of structures */
array=malloc(numstruct * sizeof *array);

/* allocate the array of pointers */
infoarray=malloc(numstruct * sizeof *infoarray);

/* have the pointers point to the structures */
for(i=0;i<numstruct;i++)
   infoarray[i]=array+i;

Tobias.



Sat, 25 Oct 2003 22:17:05 GMT  
 Sorting an Array of pointers to structures
If you have a structure like this:

  struct address
  {
     char name[FIELDWIDTH];
     char address[FIELDWIDTH];
     char city_state[FIELDWIDTH];
     char zip[FIELDWIDTH];
     char blank[FIELDWIDTH];
  };

and an array of structure pointers like this:

  struct address *array[NUMBEROFELEMENTS];

then you can write a function like:

  int compare(const void *A, const void *B)
  {
     return strcmp((*((struct address**)A))->zip,
                   (*((struct address**)B))->zip);
  }

and sort the array with the following call:

  qsort(array, NUMBEROFELEMENTS, sizeof(struct address*), compare);

Muar



Mon, 27 Oct 2003 23:44:40 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Sorting pointers to structures

2. Help using qsort to sort pointers to a structure

3. Swaping Pointers to data structures in sorting algorithm

4. Sorting an array of structures

5. Sorting Structure Array

6. arrays of structures (sorting by members)

7. sorting array of structures by string members

8. I want to be able to sort the program using pointer and structure in C progr

9. sorting an array of structures

10. Sort Array of Structures

11. Sort of an Array of Structures

12. sorting pointers to structures

 

 
Powered by phpBB® Forum Software