Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with this?

Status
Not open for further replies.

gonzilla

Programmer
Apr 10, 2001
125
US
Ok, I'm trying to sort an array of structures based on the last name member. I don't want to actually change it around physically so I want to use a pointer to it. My problem is how to "switch" up the values properly. BTW - I don't want to use the qsort() because I want to learn this. Here's the structure:

typedef struct psnger {
char LName[MAXLEN];
char FName[MAXLEN];
int SeatID;
int status;
} pseat;

pseat cust[MAXSEATS] (MAXSEATS = 12)

Here's the function I'm using.

void sort_alpha(pseat * cust, int bufsize)
{
int i, j;
pseat ** seat_s = &cust, **temp;

for (i = 0; (i < MAXSEATS && (*seat_s)->status != 0); i++)
{
for (j = i + 1; (j < MAXSEATS -1 && (*seat_s)->status != 0); j++, (*seat_s)++)
{
if (strcmp((*seat_s)->LName, ((*seat_s)+1)->LName) > 0)
{
temp = seat_s;
seat_s = seat_s + 1;
seat_s + 1 = temp;
}
}
}
}

Obviously, I'm getting an error because seat_s + 1 can't be an lvalue. I can't think of what else to do. I'm stumped. I've also tried to use malloc to do this sort which is why I'm passing the function bufsize, the size of the structure, but can't seem to get it quite right. Any help or suggestions would be great.

Thanks.

-Tyler
 
Hi Create an array of pointer to pseat like the following and assign the address of all the elements of cust to it.

pseat *p_cust[MAXSEATS];
int i;
for(i = 0; i < MAXSEATS; i++)
{
scanf(&quot;%s&quot;, cust.LName); /* just getting lname for testing.*/
p_cust = &cust;
}

pass the address of p_cust to the sort_alpha function.

sort_alpha(p_cust);
// print the result.
for(i = 0; i < MAXSEATS; i++)
{
printf(&quot;\n%s&quot;, p_cust->LName);
}

Now change the sort_alpha function like the following

void sort_alpha(pseat **seat_s)
{
int i, j;
pseat *temp;
pseat **ttemp = seat_s;
for (i = 0; (i < MAXSEATS && seat_s->status != 0); i++)
{
for (seat_s = ttemp, j = i + 1; (j < MAXSEATS && (*seat_s)->status != 0); j++, (seat_s)++)
{
if (strcmp((*seat_s)->LName, ((*seat_s) + 1)->LName) > 0)
{
temp = *seat_s;
*seat_s = *(seat_s + 1);
*(seat_s + 1) = temp;
}
}
}
}
 
Hi,

Thanks for the response. The pointer assignment works great. Thanks for the idea. I don't think the sort is working correctly though. Can you take a look at it and see why it may not be working? I was under the impression that the outer loop needs to stay constant while the inner loop starts one index higher and then replaces anything greater. This is the part that doesn't make sense to me:

for (seat_s = ttemp, j = i + 1; (j < MAXSEATS &&
(*seat_s)->status != 0); j++, (seat_s)++)

When you compare (strcmp((*seat_s)->LName, ((*seat_s) + 1)->LName) > 0) won't it just be comparing two that are right next to each other inseatd of the lowest with each?

Thanks.

-Tyler
 
I did not checked the sorting of the program. I just used your code and modified base on the pointer.

See the following one, this is working fine and may useful.
void sort_alpha(pseat **seat_s)
{
int i, j;
pseat *temp;
pseat **ttemp = seat_s;
for (i = 0; (i < MAXSEATS) && (seat_s->status != 0); i++)
{
for(j = 0; (j < MAXSEATS - i - 1) && (seat_s[j]->status != 0); j++)
{
printf(&quot;\n....%d&quot;, j);
if (strcmp(seat_s[j]->LName, seat_s[j + 1]->LName) > 0)
{
temp = seat_s[j];
seat_s[j] = seat_s[j + 1];
seat_s[j + 1] = temp;
}
}
}
}

Maniraja S
 
Thanks again. I really appreciate your help. I was trying to figure a way to do it without using the indexes and [j]. I'll work on it some more, but it does seem to work just fine.

Thanks.

-Tyler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top