# Thread: need help on structure

1. ## need help on structure

hi i have a program and i want to sort the numbers each time i will input a number up to 5 numbers.
Code:
```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void sort(struct student info);

struct student
{
int gpa[5];
char name[30];
};

main()
{     int i=0;
int count;
struct student info;

for(i=0;i<5;i++)
{
printf("enter gpa");
scanf("%i",&info.gpa[i]);

}

getch();

}
void sort(struct student info)
{
int temp ;
int array;
int j;
int i;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(array[i] > array[j])  //normal is descending sort, so I changed it for you
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
printf("\n\n");
for(i=0;i<6;i++)
{
printf(" %f ",info.gpa[i]);
}
getch();

}```

2. > i want to sort the numbers each time i will input a number up to 5 numbers.
So create a separate sort function, which is outside of the menu function (which is an odd place for it anyway).

Then you can call it from wherever you want.

3. You also have a mistake in the first for loop. i cannot go all the way to 4, because if it does so, j = i+1 will make j out of bounds. i < 4 is the correct condition

4. so what is my mistake

5. i cannot go all the way to 4, because if it does so, j = i+1 will make j out of bounds. i < 4 is the correct condition

6. i dont get what u are sayingi change it to 4 but it still wont run

7. You are declaring array as a simple int instead of an array.

8. Also your logic doesn't make any sense. WHAT IS IT you are actually trying to sort?

9. iam tryint to enter 5 number into gpa and the pass it into the function and the sort it. iam lost that is why my code doesnt make sense

10. Ok, here are a few things for you to work on:

1) You are passing a student structure to the sort function but are not using it anywhere inside. My guess is that you want to sort the gpa[] array contained in that student structure. To do this you will need to pass a pointer to the structure rather than the structure itself, so change the sort function header to this:

Code:
```void sort(struct student *info){
/* contents */
}```
2) Now inside the sort function remove the array variable. You are now sorting the gpa[] array member of info. So replace array[i], array[j] with info->gpa[i], info->gpa[j].

3) I dont know what menu(info) does in your main. That function is not defined anywhere or you just haven't included it's definition in the posting.

4) After you input the student data in main, make sure to call sort(&info); This will call the function sort and pass a pointer to info (which is the argument type sort() now accepts).

Good luck! Let us know how if you have any other problems.

11. is there anyway to do it with out using pointers because i dont know how to use pointer yet

12. If you want to modify the variable you are passing as an argument to the function, no, there is no other way.

Alternatively you can move everything in the sort function in your main if you don't want to/not allowed to use pointers.

If not, just modify the code according to the instructions above. You don't really need a lot of knowledge about pointers at this point, only that a pointer is a memory address. So instead of passing the values contained in structure info to the sort function you are passing the memory address of where that structure resides in memory and then modify it there, so that the changes are reflected in the variable info in main after the function sort is exited.

13. ## wont sort

i think i go it

14. ## cant sort

i did something like this and it wont sort is my code possbile
Code:
```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void pass (struct student information);
struct student
{
int mark[5];
int name[30];
};
main()
{     struct student information;
int i;

for(i=0;i<5;i++)
{
printf("enter mark %i:",i+1);
scanf("%i",&information.mark[i]);
}
pass (information);
getch();
}
void pass (struct student information)
{         int temp;
int i;
int j;

for(i=0;i<5;i++)
{
for(j=i+1;i<5;i++)
{
if(information.mark[i]<information.mark[j])
{
temp=information.mark[i];
information.mark[i]= information.mark[j];
information.mark[j]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%i",information.mark[i]);
}
}```

15. As I previously said twice, it will never sort because you are not passing the address of the struct in your sort function but just the values contained therein. You need to pass the pointer to the struct if you want to modify it (i.e. sort it) inside another function.

Pass struct student *info to the function and call it in main as sort(&info).

Also change all your dots when accessing members to ->