emre said:
I used a structer in this program.
typedef struct _guitar {
I recommend not using an underscore here, as identifiers beginning with
an underscore (or double underscores) may be used by the
implementation.
int serial; :serial of item at the guitar store
int price;

rice of item at the guitar store
unsigned would be a better choice as both values are not going to be
negative.
char builder[18]; :builder of item at the guitar store
char type[18]; :type of item at the guitar store
char model[18]; :model of item at the guitar store
These arrays are fine if you are absolutely certain that the data you
are going to put into them will never exceed their bounds. Otherwise
you need to either declare them of sufficient size or, instead of
arrays, make 'builder', 'type' and 'model' to be pointers to char and
allocate memory dynamically with malloc(). This allows any arbitrary
sized data to be stored with either buffer overflow or memory wastage.
Nor do you need to know these details in advance.
}guitar;
I want to list according to alphabetic by model. How can I do, do you
know any algorithm?
I presume you have an array of these structures? You can easily sort
them based on the value of 'model' by using the Standard library
function qsort(). You need to provide it a "comparison function"
though. This function will be called by qsort() with two elements of
the array to be sorted, and it must return an int value less than,
equal to, or greater than zero according to whether it's first argument
is less than, equal to, or greater than it's second argument. How
exactly you do this comparison will of course depend on the exact
format of the array members and your sorting criteria.
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_GUITARS 3
struct guitar {
int serial;
int price;
char builder[18];
char type[18];
char model[18];
};
int sort_guitar(const void *n, const void *m) {
return strcmp(((struct guitar *)n)->model,
((struct guitar *)m)->model);
}
void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
unsigned ctr;
fprintf(s, "Guitars:\n========\n\n");
for (ctr = 0; ctr < num; ctr++) {
fprintf(s, "Serial:\t\t%d\nPrice:\t\t%d\nBuilder:\t%s\nType:\t\t"
"%s\nModel:\t\t%s\n\n", (arr+ctr)->serial,
(arr+ctr)->price, (arr+ctr)->builder, (arr+ctr)->type,
(arr+ctr)->model);
}
return;
}
int main(void) {
struct guitar arr[NUM_GUITARS] = {
{ 1, 1000, "Michealangelo", "foo", "MM111" },
{ 2, 920, "Leonardo", "bar", "ML112" },
{ 3, 1500, "Raphael", "baz", "MR123" } };
print_guitars(arr, NUM_GUITARS, stdout);
qsort(arr, NUM_GUITARS, sizeof arr[0], sort_guitar);
print_guitars(arr, NUM_GUITARS, stdout);
return 0;
}
$ gcc -Wall -Wextra -ansi -pedantic -o guitars guitars.c
$ ./guitars
Guitars:
========
Serial: 1
Price: 1000
Builder: Michealangelo
Type: foo
Model: MM111
Serial: 2
Price: 920
Builder: Leonardo
Type: bar
Model: ML112
Serial: 3
Price: 1500
Builder: Raphael
Type: baz
Model: MR123
Guitars:
========
Serial: 2
Price: 920
Builder: Leonardo
Type: bar
Model: ML112
Serial: 1
Price: 1000
Builder: Michealangelo
Type: foo
Model: MM111
Serial: 3
Price: 1500
Builder: Raphael
Type: baz
Model: MR123
$