Start by indenting your code to make it readable - at the moment its not easy to see what is going on at all.
Then replace all the single character variables with descriptive names:
s
is meaningless as a variable name as it tells you nothing about what it might contain
if(strcmp(s[i].name, s[j].name) > 0){
Compared with
if(strcmp(students[i].name, students[j].name) > 0){
And calling your
struct
List is misleading as well - it isn't a list of anything, it's a Student's detail - so calling it a Student instead makes your code more self documenting.
Then think about what you are doing.
if(strcmp(s[i].name, s[j].name) > 0){
strcpy(temp, s[i].name);
strcpy(s[i].name, s[j].name);
strcpy(s[j].name, temp);
What happens to the Id's when you swap the names over?
A better solution would be to make the Students collection an array of pointers to List objects, and swapping pointers instead of copying strings - it's a whole load more efficient, easier to read, and more obviously correct because you can't "separate" ID and names at any time (Hint: you will need
malloc
to allocate the space for each List object).
Quote:
But how can I only print the name on screen? If I not separate them?
If you have them in a
struct
then you have access to both of them via the
struct
members:
#include<stdio.h>
typedef struct
{
int Count;
float Price;
} Item;
int main()
{
printf("Hello World");
Item bike;
bike.Count = 3;
bike.Price = 123.45;
Item car;
car.Count = 1;
car.Price = 43995.00;
printf("We have %u bikes in stock, at £%0.2f\n", bike.Count, bike.Price);
printf("We have %u cars in stock, at £%0.2f\n", car.Count, car.Price);
return 0;
}