1

I just started learning C and I'm still new to it. In this program I'm working with an array of structs. The structs are:

typedef struct {
    int day;
    int month;
    int year;
} Date;

typedef struct {
    int serial_num;
    char full_name[15];
    Date *pDate;
} Person;

The array is Person *people.

Now I have two arrays of people and birth dates of those people (same indexes):

const char* names[MAX] = { "Sasson_Sassoni", "Pooh", "James_Bond", "Elvis_is_Alive", "Shilgiya", "Cleopatra", "Sissoo_VeSimmhoo" };

const int dates[MAX][COLS] = {
        { 10, 1, 1988 },
        { 12, 12, 1948 },
        { 4, 12, 1970 },
        { 11, 11, 1890 },
        { 11, 11, 1948 },
        { 1, 10, 1213 },
        { 12, 11, 1948 }
    };

By using switch case, every time the user types 1 a person from the lists (Name and birthday) is added to the list people. Then if the user types 3, the list people should be sorted by date (oldest to youngest). So I wrote the following two functions:

void sortList(Person **people, int index) {
    qsort(*people, index, sizeof(Person), intcmp);
}
int intcmp(const void *a, const void *b) {
    Person *one = (Person *)a;
    Person *two = (Person *)b;
    int year1 = one->pDate->year;
    int year2 = two->pDate->year;
    int month1 = one->pDate->month;
    int month2 = two->pDate->month;
    int day1 = one->pDate->day;
    int day2 = two->pDate->day;
    if (year1 > year2)
        return -1;
    else if (year2 > year1)
        return 1;
    if (month1 > month2)
        return -1;
    else if (month2 > month1)
        return 1;
    if (day1 > day2)
        return -1;
    else if (day2 > day1)
        return 1;
    return 0;
}

But every time I get an error saying:

Exception thrown: read access violation.
one->pDate was nullptr.

Any help? Thanks!

EDIT: Further explanation: In order to insert the people to the array one by one, I made a variable called index and every time a person is added the index grows by one. So When calling the function qsort(), index is the number of people in the array. Also MAX=7, COLS=3, LEN=10. The function that adds people to the array is:

void addToList(Person **people, int *index, const char *names[MAX], const int dates[][COLS]) {
    people[*index] = (Person *)malloc(sizeof(Person));
    people[*index]->serial_num = *index + 1;
    strcpy(people[*index]->full_name, names[*index]);
    Date *temp = (Date *)malloc(sizeof(Date));
    temp->day = dates[*index][0];
    temp->month = dates[*index][1];
    temp->year = dates[*index][2];
    people[*index]->pDate = temp;
    printf("%d %s     %d/%d/%d \n", people[*index]->serial_num, people[*index]->full_name, people[*index]->pDate->day, people[*index]->pDate->month, people[*index]->pDate->year);
    *index = *index + 1;
}
12
  • Please provide a minimal reproducible example. What is the value of index for example, what is people ? Commented Oct 10, 2017 at 16:05
  • 1
    @Stargateur - Kinda jumped that gun, don't you think? This message is from the run-time. It has nothing to do with the language this is being compiled as. Commented Oct 10, 2017 at 16:16
  • 1
    @eitanmayer - To help us figure this out once and for all, does char *c = malloc(1); compile as is in your program, or do you have to add a cast? Commented Oct 10, 2017 at 16:23
  • 3
    @eitanmayer - The compiler used can affect the semantics! Use a C compiler, not a C++ one. If you haven't yet, switch your project to C mode. Commented Oct 10, 2017 at 16:27
  • 1
    You should have a function which prints out the data in your array. You should use that function before calling the sort to ensure your data is set up correctly. Then you can be more confident about what your sorting is up to. Commented Oct 10, 2017 at 16:42

1 Answer 1

3

Your mcve is not complete but I think it's because you confuse pointer and struct:

void sortList(Person **people, int index) {
    qsort(people, index, sizeof(Person *), intcmp);
    // or qsort(people, index, sizeof *people, intcmp);
}

int intcmp(const void *a, const void *b) {
    const Person *one = *(const Person **)a;
    const Person *two = *(const Person **)b;
Sign up to request clarification or add additional context in comments.

2 Comments

Wow that worked! I've been stuck on this a while, thanks!
BTW, good use of const and not using Person *one = *(Person **)a;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.