2

I'm a newbie with C language and I need to make a function to sort an array of struct Student data types (after the element Student.ime alphabetically). I am not really sure where to put the pointers so I can return the new array.

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

struct Student {
    int id;
    char ime[20];
    char prezime[20];
    char brindexa[20];
    struct Datum datum_rodjenja;
};

struct Student sortiraj(struct Student niz[], int vel)
{
    int i, j;
    struct Student tempo;
    tempo = niz[0];
    for(j=0 ; j<vel ; j++)
    {
        for(i=j ; i<vel ; i++)
        {
            if(strcmp(tempo.ime,niz[i].ime)>0)
            {
                tempo = niz[i];
            }
            i++;
        }
        niz[j] = tempo;
        j++;
    }
    return niz;
}

The array is stored in a .txt file but that is not the problem. One more thing, how do I call the function in the main(). I thought maybe like this?

niz=sortiraj(niz, vel);

Can someone give me any tips please. Thank you.

3
  • A newbie that is able to indent code - this is refreshing and gets a +1 Commented Jan 20, 2014 at 23:22
  • sortiraj incorrect. Why not use the qsort? Commented Jan 20, 2014 at 23:24
  • have you considered qsort? Commented Jan 20, 2014 at 23:26

5 Answers 5

2

You seem to be sorting the array in-place, so you don't really need to return it at all. Also, use the standard library function qsort():

int cmp(const void *ap, const void *bp)
{
    const struct Student *a = ap, *b = bp;
    return strcmp(a->ime, b->ime);
}

struct Student students[] = { /* whatever */ };
qsort(
    students,
    sizeof(students) / sizeof(studends[0]),
    sizeof(students[0]),
    cmp
);

Also, please use English function and variable names.

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

2 Comments

Thank you for the help. I will use english from now on, I just registeres so I forgot about that. I'll try to implement this to my code.
@H2CO3 - Your answer more accurately and precisely answers the question (sorts array of struct). Not sure why I got the check mark. Nice job as usual. +1
0

Have you considered using the qsort() function? Example:

If you have an array of strings say strings, (Note: not an array of structs), with the number of strings being say, cnt then:

qsort(strings, cnt, sizeof(char*), sortstring);  

With the function sortstring defined as:

static int sortstring( const void *str1, const void *str2 )
{
    const char *rec1 = *(const char**)str1;
    const char *rec2 = *(const char**)str2;
    int val = strcmp(rec1, rec2);

    return val;
}

//for a simple test, run this main with the code above:  

int main(void)  
{  
    char *strings[]={"this", "is", "a", "test", "of", "the", "qsort", "function", "to", "try"};
    int strlen = sizeof(strings)/sizeof(char *);

    qsort(strings, strlen, sizeof(char *), sortstring);

    return 0;
}  

//Note my environment required #include <ansi_c.h>

Comments

0

First of all, your function signature is not marked return type as an Array of Students. Second of all, I don't know which sorting algorithm you're trying to use, but your implementation isn't right.

If you correct your function signature, you shouldn't get any error by the way you're calling the:

struct Student* sortiraj(struct Student niz[], int vel)

tips about your sortiraj: Check either selection sort or bubble sort for your sort algorithm. And for further study, you can check some recursive algorithms like merge sort and quick sort which are more advance and you need more programming knowledge to implement them.

Comments

0
  1. Change the return type of function sortiraj to void.

  2. Create and fill the struct Student niz[] array in function main.

  3. From function main, pass the array and its length (vel) to function sortiraj.

  4. And of course, no need to return anything from function sortiraj (as implied in section 1).

Comments

0

Since you are passing in a pointer, namely niz, you are changing the memory that niz points to. This means that you don't have to return niz.

However if you want to return niz your function's return type must be the same type as niz. Currently you have a return type of just struct Student so you should be getting a compilation error.

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.