I have managed to get this most recent project of mine to compile and run just fine, the data displays as it should, however, the purpose of this project was to sort structs (DBrecords) in various ways. Unfortunately I must not be understanding how to swap which structure the pointer is assigned to correctly. I've tried a few methods, such as memcpy...that failed miserably as I don't fully know how to use that (tried sizeof(DBrecord) as size_t... set fault). I'll post the code below.

Some Notes: DBrecord is name of structure,
instructor wanted the student's ID to be a char array, I may have scanned that in wrong, should I have another loop to go and look of %c rather than use %s since the ID is all numerical in the data file? and how would I get scans to ignore the space between the name and id?


program header file:

Code:
#include <stdio.h>
#include <stdlib.h>    


    typedef enum {
        firstYear = 0,
        sophomore,
        junior,
        senior,
        grad
    }Year;


    typedef struct {
        int recordid;
        char *lastname;
        char *firstname;
        char ID[8];
        int age;
        Year year;
        float GPA;
        int gradYear;
    } DBrecord;

Sort functions file:

Code:
#include "major4.h"#include <string.h>


void swap(void *ptra, void *ptrb)
{


    void *temp;


    temp = ptra;
    ptra = ptrb;
    ptrb = temp;
    




    
}


void sortFirst(DBrecord *records[], int recordct)
{


    int i;


    for (i = 0; i < recordct; i++) {


        if (strcmp((records[i]->firstname), (records[i + 1]->firstname)) > 0) swap(records[i], records[i + 1]);


    }










   
}


void sortLast(DBrecord *records[], int recordct)
{


    int i;


    for (i = 0; i < recordct; i++) {


        if (strcmp((records[i]->lastname), (records[i + 1]->lastname)) > 0) swap(records[i], records[i + 1]);


       
    }




}


void sortAge(DBrecord *records[], int recordct)
{


    int i;


    for (i = 0; i < recordct; i++) {


        if ( (records[i]->age) > (records[i + 1]->age)) swap(records[i], records[i + 1]);


    }
    
}


void sortGPA(DBrecord *records[], int recordct)
{


    int i;


    for (i = 0; i < recordct; i++) {


        if ((records[i]->GPA) < (records[i + 1]->GPA)) swap(records[i], records[i + 1]);


    }




   
}


void sortClass(DBrecord *records[], int recordct)
{


    int i;


    for (i = 0; i < recordct; i++) {


        if ((records[i]->year) > (records[i + 1]->year)) swap(records[i], records[i + 1]);


    }


    
}


void sortGrad(DBrecord *records[], int recordct)
{


    int i;


    for (i = 0; i < recordct; i++) {


        if ((records[i]->gradYear) > (records[i + 1]->gradYear)) swap(records[i], records[i + 1]);


    }
    
}


void sortID(DBrecord *records[], int recordct)
{


    int i;


    for (i = 0; i < recordct; i++) {




        if (strcmp(records[i]->ID, records[i + 1]->ID) > 0) swap(records[i], records[i + 1]);


    }


    
}


void printyear(Year year)
{


    switch (year) {


    case firstYear: printf("firstYear ");
        break;
    case sophomore: printf("sophomore ");
        break;
    case junior: printf("junior ");
        break;
    case senior: printf("senior ");
        break;
    case grad: printf("grad ");
        break;






    }




   
}


void printstruct(DBrecord *records[], int recordct)
{


    int i;


    for (i = 0; i < recordct; i++) {


        printf("%s, %s: %s %d ", records[i]->lastname, records[i]->firstname, records[i]->ID, records[i]->age);
        printyear(records[i]->year);
        printf("%f %d\n\n", records[i]->GPA, records[i]->gradYear);


    }


    
}

main file which calls the functions:

Code:
/*  * File:   driver.c
 * Author: Alexander Hollis
 *
 * Created on April 3, 2012, 6:41 PM
 */


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


/*
 * 
 */
int main()
{


    printf("CSE1040 Major 3 Assignment - Alex Hollis - [email protected] - Tested on CSP 01\n\n");


    int numrecords;


    scanf("%d", &numrecords);




    DBrecord * records[numrecords];


    int i;


    for (i = 0; i < numrecords; i++) {


        records[i] = (DBrecord *) malloc(sizeof (DBrecord));


        records[i]->lastname = (char *) malloc(sizeof (scanf("%s")));
        scanf("%s", records[i]->lastname);
        records[i]->firstname = (char *) malloc(sizeof (scanf("%s")));
        scanf("%s", records[i]->firstname);
        scanf("%s", records[i]->ID);
        scanf("%d", &records[i]->age);


        char testdata[10];


        scanf("%s", testdata);


        if (strcmp(testdata, "firstYear") == 0) records[i]->year = firstYear;
        else if (strcmp(testdata, "sophomore") == 0) records[i]->year = sophomore;
        else if (strcmp(testdata, "junior") == 0) records[i]->year = junior;
        else if (strcmp(testdata, "senior") == 0) records[i]->year = senior;
        else records[i]->year = grad;


        scanf("%f", &records[i]->GPA);
        scanf("%d", &records[i]->gradYear);


    }




    printf("Sorted by Last Name:\n");
    sortLast(records, numrecords);
    printstruct(records, numrecords);


    printf("Sorted by First Name:\n");
    sortFirst(records, numrecords);
    printstruct(records, numrecords);


    printf("Sorted by Student ID:\n");
    sortID(records, numrecords);
    printstruct(records, numrecords);


    printf("Sorted by Age:\n");
    sortAge(records, numrecords);
    printstruct(records, numrecords);


    printf("Sorted by Class:\n");
    sortClass(records, numrecords);
    printstruct(records, numrecords);


    printf("Sorted by GPA:\n");
    sortGPA(records, numrecords);
    printstruct(records, numrecords);


    printf("Sorted by Expected Graduation Data:\n");
    sortGrad(records, numrecords);
    printstruct(records, numrecords);










    return (EXIT_SUCCESS);
}

If you see anything else that could cause problems when more students are added, up to 320... I appreciate all feedback.

Thank You.

- Alex