#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define COUNT(x) (sizeof(x) / sizeof(x[0]))
struct entry {
const char *word;
const char *def;
};
int compare_entry(const void *l, const void *r)
{
const struct entry *ll = l;
const struct entry *rr = r;
return strcmp(ll
->word
, rr
->word
); }
void dump(struct entry dict[], size_t n)
{
for (size_t i = 0; i < n; i++) {
printf("\"%s\" = \"%s\"\n", dict
[i
].
word, dict
[i
].
def); }
}
int main()
{
struct entry dictionary[] = {
{ "def", "second entry" },
{ "abc", "first entry" },
{ "ghi", "third entry" },
{ "mno", "fifth entry" },
{ "jkl", "fourth entry" }
};
dump(dictionary, COUNT(dictionary));
qsort(dictionary
, COUNT
(dictionary
), sizeof(dictionary
[0]), compare_entry
);
dump(dictionary, COUNT(dictionary));
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHVuaXN0ZC5oPgojaW5jbHVkZSA8c3RyaW5nLmg+CgojZGVmaW5lIENPVU5UKHgpIChzaXplb2YoeCkgLyBzaXplb2YoeFswXSkpCgpzdHJ1Y3QgZW50cnkgewogICAgY29uc3QgY2hhciAqd29yZDsKICAgIGNvbnN0IGNoYXIgKmRlZjsKfTsKCmludCBjb21wYXJlX2VudHJ5KGNvbnN0IHZvaWQgKmwsIGNvbnN0IHZvaWQgKnIpCnsKICAgIGNvbnN0IHN0cnVjdCBlbnRyeSAqbGwgPSBsOwogICAgY29uc3Qgc3RydWN0IGVudHJ5ICpyciA9IHI7CiAgICByZXR1cm4gc3RyY21wKGxsLT53b3JkLCByci0+d29yZCk7Cn0KCnZvaWQgZHVtcChzdHJ1Y3QgZW50cnkgZGljdFtdLCBzaXplX3QgbikKewogICAgZm9yIChzaXplX3QgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBwcmludGYoIlwiJXNcIiA9IFwiJXNcIlxuIiwgZGljdFtpXS53b3JkLCBkaWN0W2ldLmRlZik7Ci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