PHP Code:
#include <st***.h>
#include <stdlib.h>
#include <string.h>
#define ELEMENTS(array) (sizeof(array)/sizeof(array[0]))
#define NUMBER 2
typedef struct {
char * nome;
char * cognome;
unsigned int esami;
float media;
} studente_struct;
int compara_struct_nome (const void * a , const void * b);
void stampa_struct ( studente_struct * a);
int main ( void ) {
char buffer[50];
int i;
studente_struct studente[NUMBER];
for (i=0;i<NUMBER;i++){
printf ("Nome\n");
scanf ("%49s",buffer);
(*(studente +i)).nome= strdup(buffer);
printf ("Cognome\n");
scanf ("%49s",buffer);
(*(studente +i)).cognome= strdup(buffer);
printf ("Esami\n");
scanf ("%u",&((*(studente+i)).esami) );
printf ("Media\n");
scanf ("%f",&((*(studente+i)).media) );
}
////////////////////////////// ordina per nome
qsort(studente , ELEMENTS(studente) , sizeof(studente[0]), compara_struct_nome);
stampa_struct (studente);
/////////////////////////////////
return 0;
}
int compara_struct_nome (const void * a , const void * b){
studente_struct * dat1 = ( studente_struct *) a;
studente_struct * dat2 = (studente_struct *) b;
return strcmp ( (*(dat1)).nome , (*(dat2)).nome );
}
void stampa_struct ( studente_struct * a){
int i;
for(i=0;i<NUMBER;i++){
printf ("%s\n", (*(a + i)).nome);
printf ("%s\n", (*(a + i)).cognome);
printf ("%u\n", (*(a + i)).esami);
printf ("%f\n", (*(a + i)).media);
}
}