Thread: Sorting Structs Program Help

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    27

    Sorting Structs Program Help

    Hello All,

    I hope everyone is doing well. I have a program that is supposed to accept a list of addresses via input out redirection in the command prompt and then output them organized from lowest to highest in zip-code order. The program has a minimum of two entries and no maximum. Here is what i have below. I'm not getting any errors in VS but when i run the program through the command prompt it seems to crash.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct storeaddress
    {
    	char fandlname[50];
    	char street[50];
    	char cityandstate[25];
    	int zip;
    };
    
    
    struct storeaddress*createaddress();
    void printaddr(int ptrct, struct storeaddress*ptr[]);
    void sortingzip(int ptrct, struct storeaddress*ptr[]);
    
    
    int main()
    {
    	struct storeaddress *ptr[50];
    	int ptrct = 0;
    	char buf[50];
    	do
    	{
    		ptr[ptrct] = createaddress();
    		ptrct++;
    	} while (((fgets(buf, 50, stdin) != NULL) && (ptrct < 3)));
    	{
    		sortingzip(ptrct, ptr);
    		printaddr(ptrct, ptr);
    	}
    }
    
    
    struct storeaddress*createaddress()
    {
    	struct storeaddress*newaddress = (storeaddress*)malloc(sizeof(struct storeaddress));
    	fgets(newaddress->fandlname, 50, stdin);
    	fgets(newaddress->street, 50, stdin);
    	fgets(newaddress->cityandstate, 50, stdin);
    	scanf("%d", &newaddress->zip);
    	return newaddress;
    };
    
    
    void printaddr(int ptrct, struct storeaddress *ptr[50])
    {
    	int j;
    	for (j = 0; j < ptrct; j++)
    	{
    		printf("\n%s\n%s\n%s\n%s\n%d", ptr[j]->fandlname, ptr[j]->street, ptr[j]->cityandstate, ptr[j]->zip);
    	}
    
    
    };
    
    
    void sortingzip(int ptrct, struct storeaddress *ptr[])
    {
    	int i, k;
    	for (i = 0; i < ptrct + 1; i++)
    	{
    		for (k = 0; k < ptrct + 1; k++)
    		{
    			if (ptr[k]->zip > ptr[k + 1]->zip)
    			{
    				struct storeaddress *temp = ptr[k + 1];
    				ptr[k + 1] = ptr[k];
    				ptr[k] = temp;
    			}
    		}
    	}
    };
    I want to thank you in advance for any help/advice you can offer, i appreciate it.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'm not getting any errors in VS
    Then you probably need to increase your compiler warning levels. Look at what my compiler says about your code:

    Code:
    main.c||In function ‘createaddress’:|
    main.c|39|error: ‘storeaddress’ undeclared (first use in this function)|
    main.c|39|note: each undeclared identifier is reported only once for each function it appears in|
    main.c|39|error: expected expression before ‘)’ token|
    main.c|45|error: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic]|
    main.c||In function ‘printaddr’:|
    main.c|53|warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘int’ [-Wformat=]|
    main.c|53|warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]|
    main.c|57|error: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic]|
    main.c|75|error: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic]|
    You really should stop casting the return value of malloc(), this can mask some serious problems. You probably should find and study some documentation for malloc() and insure you are using it properly.



    Jim

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    We would normally typedef the struct so we don't have to write "struct storeaddress" all the time.

    You say it's supposed to have no maximum but you've limited it to 50 entries. Then you further limit it to only reading in 3 entries (maybe that's for debugging purposes).

    ptr is a terribly non-descriptive name! sa would be better. Or addr. Or store. Or storeAddr. Pretty much anything is better than ptr!

    You're not controlling the input loop correctly. That's probably the ultimate cause of the segfault. It's better to have createAddress return NULL if it detects the end of the file. And shouldn't it be called something like readAddress?

    It's better to read the zipcode line as a string with fgets and then use sscanf on the string to extract the int. Or you could save the zipcode as a string since it's not really a number (it's composed of numerals but you don't add or multiply them).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explorations in Structs and sorting.
    By jimtuv in forum C Programming
    Replies: 52
    Last Post: 06-17-2010, 11:04 PM
  2. Sorting a vector of structs....
    By ropitch in forum C++ Programming
    Replies: 7
    Last Post: 07-10-2009, 11:49 PM
  3. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  4. Sorting Vector of structs.
    By Zosden in forum C++ Programming
    Replies: 41
    Last Post: 10-22-2008, 12:28 AM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM

Tags for this Thread