Hey guys this is my first post here and i was wondering if any of you can help me figure out how to sort array of pointers to structures. Here's my code and here's my assignment if anyone is interested https://i.stack.imgur.com/Z9uUO.png.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
struct address
{
char name[50];
char street[50];
char citystate[50];
char zip[20];
};
int main()
{
struct address *ptr[50];
struct address tptr;
char buffer[80];
int count = 0;
for (int i = 0; i <MAX; i++)
{
ptr[i] = malloc(sizeof(struct address));
if (gets(buffer)== NULL)
{
break;
}
else
{
strcpy((*ptr[i]).name, buffer);
gets((*ptr[i]).street);
gets((*ptr[i]).citystate);
gets((*ptr[i]).zip);
free(ptr[i]);
count++;
}
}
for (int x = 0; x<count; x++)
{
for (int y = 0; y<count - 1; y++)
{
if ((*ptr[y]).zip>(*ptr[y + 1]).zip)
{
tptr = ptr[y + 1];
ptr[y + 1] = ptr[y];
ptr[y] = tptr;
}
}
}
for (int i = 0; i < count; i++)
{
puts((*ptr[i]).name);
puts((*ptr[i]).street);
puts((*ptr[i]).citystate);
puts((*ptr[i]).zip);
}
}