0

I read a text file and stored the data in a struct array, then tried to sort it and to print the un-sorted and sorted arrays. It works up to reading the array text and storing in the array, but the sorting function does not run without giving any errors. what is wrong with it? I used the strcmp() function to compare student[s]:

#include <stdio.h>

struct person { //struct person with 4 fields
    char name[100];
    char address[100];
    char IDnumber[20];
    int  age;
};

int main (void) {
    FILE *file = fopen ( "personout.txt", "r");
    struct person student[10]; // declares an struct array to store data
    int k = 0;
    if (file != NULL) {
        char line[300];
        while ( k < 10 && fgets ( line, sizeof line, file ) != NULL ) {
            if ( 4 == sscanf ( line, " %99[^,], %99[^,], %19[^,], %d", 
                student[k].name, student[k].address, student[k].IDnumber, &student[k].age))
            {
                printf ( "%s\n Un-sorted array");
                printf ( "%s\n", student[k].name);
                printf ( "%s\n", student[k].address);
                printf ( "%s\n", student[k].IDnumber);
                printf ( "%d\n", student[k].age);
                k++;
            }
        }
        fclose ( file );
    }
    sortarray();

    // prints sorted array
    printf ( "%s\n Sorted array");
    for (int t=0;t<10;t++) {
        printf ( "%s\n", student[t].name);
        printf ( "%s\n", student[t].address);
        printf ( "%s\n", student[t].IDnumber);
        printf ( "%d\n", student[t].age);
        t++;
    }
}

void sortarray(){
    // number of records a  r=a first for loop
    // inner for loop s=r+1
    struct person temp,student[10];
    int a=10;
    for (int r=0;r<a-1;r++) {
        for (int s=r+1;r<a;s++) {
            if (strcmp(student[r].name, student[s].name) > 0) {
                temp = student[r];
                student[r] =student[s];
                student[s] = temp;
            }
        }
    }
}
2
  • @gs Gunasekara What array is being sorted by the function?! Commented Sep 30, 2019 at 13:20
  • printf ( "%s\n Un-sorted array") and printf ( "%s\n Sorted array")??? Take some time to explain those calls to your rubber duck. Commented Sep 30, 2019 at 13:20

1 Answer 1

2

Besides the undefined behavior you have from two of your printf calls, you have a problem with local variables actually being local.

The student array in the main function is not the same student array in the sortarray function. The array in the sortarray function will be uninitialized and attempting to sort it will lead to undefined behavior.

The solution is to pass a pointer to the first element of the array from the main function, so the sortarray function can use it. That needs three changes:

  1. Add a forward declaration of the sortarray function:

    void sortarray(struct person *);
    
  2. Call the function passing a pointer to the first element:

    sortarray(student);  // As arrays decays to pointers, this is like passing &student[0]
    
  3. Remove the definition of the student array in the sortarray function.

Sign up to request clarification or add additional context in comments.

Comments

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.