struct  student
{
    char name[20];
    int age;
    int id;
};
struct student *sp, st[3],temp
;

void sort(struct student *p)
{
	*sp=st[0];
	p->age;

for(i=0; i<3; i++)
	{
		if(st[i].age<st[i+1].age)
  		continue;
      	 else
		if(st[i].age>st[i+1].age)
		{
			temp=st[i];
			st[i]=st[i+1];
			st[i+1]=temp;
		}break;
  	}	
}

/* switch case junk */
case 1: /* sort by age */
sort(sp);
continue;

ok, i think i have the alogorithm correct now, b/c it works when i put directly into a case under the switch. yet, i have failed to understand what to do with pointers and all that to pass the structures through the function. sry, about the multiple posts, im sure u guys are getting just as annoyed as i am now :-|

you use the pointers for efficiency.
This way you are passing just a memory address where the struct can be found rather than the struct itself.

As a side effect you can directly manipulate the content of the struct which would otherwise be impossible as you'd be passing a copy of the struct rather than the struct itself.

you use the pointers for efficiency.
This way you are passing just a memory address where the struct can be found rather than the struct itself.

As a side effect you can directly manipulate the content of the struct which would otherwise be impossible as you'd be passing a copy of the struct rather than the struct itself.

ok, that makes sense, but im still lost on what i need to do.

if you can't explain what you need/want to do how can you expect us to tell you how to do it?

My bad, whats going to happen is the user is going to be prompted to input the information for three students, inlucing: their name, age, and id. After they havfe inputed all that data, they are asked which sorting method they would like to use(switch case- sort by their name alphabetically, or their age or id in increasing order). what i need to do is take the info that they inputed and send it to the function named sort, there the info (only age is shown in my code at the moment) is sorted. then it will return a chart type thing displaying the sorted data. So, the structures that created in the beginning each have to be tested and moved around based on the age of the student. what i want to know is where this segmentation error is occuring, and how to fix it.

if you can't explain what you need/want to do how can you expect us to tell you how to do it?

NVM, i got it kinda.

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


int i,k,j,type;
char buffer[100],l,line[50];


struct  student /*structure*/
{
char name[20];
int age;
int id;
};


struct student *sp, *st[5], *temp;



void sort(struct student *p) /* function for sorting by age */
{
sp=st[0];
p->age;
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if(st->age<st[j]->age)
continue;
else
temp=st;
st=st[j];
st[j]=temp;
}
}
}


int main()
{
for(i=0;i<5;i++)
{
st = malloc(sizeof(struct student));
}
for(i=0;i<5;i++)
{
printf("please enter student's name:\n");
fgets(st->name,sizeof(st->name),stdin);
st->name[strlen(st->name)-1]='\0';
printf("please enter student's age:\n");
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer,"%d",&st->age);
printf("please enter student's id:\n");
fgets(buffer,sizeof(buffer),stdin);
sscanf(buffer,"%d",&st->id);
}
while(1)
{
printf("Please enter the # for the program you want to run:\n");
fgets(line,sizeof(line),stdin);
sscanf(line,"%d",&type);
if(type==5)
break;
switch(type)
{


case 1:/*sort by age*/
sort(sp);
printf("Name\t\tage\t\tid\n");
printf("-----------------------------------------\n");
for(k=0;k<5;k++)
printf("%s\t\t%d\t\t%d\n",st[k]->name,st[k]->age,st[k]->id);
continue;

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.