I have been working my way through Kochan's "Programming in C" 3rd Edition on my own to get ready for grad school next year, and I am stuck for the first time. I am a chapter away from pointers, yet the exercise at the end of this most recent chapter on character strings has a problem that my own research seems to indicate can only be solved by using pointers.
The question has to do with a data structure entry:
struct entry {
char word[15];
char definition[50];
};
We create an array of entries:
struct entry dictionary[10] =
{{"aardvark", "a burrowing African mammal"},
{"abyss", "a bottomless pit"},
{"addle", "to become confused"},
{"aerie", "a high nest"},
{"ajar", "partially opened"},
{"acumen", "mentally sharp; keen"},
{"affix", "to append; attach"},
{"agar", "a jelly made from seaweed"},
{"ahoy", "a nautical call of greeting"},
{"aigrette", "an ornamental cluster of feathers"}};
The prompt reads: "Write a function called dictionary_sort that sorts a dictionary, as defined [above], into alphabetical order."
I know there are subtleties to structures and arrays in relation to functions and how functions can take them as arguments or give them back as returned values. The only way that seemed to make sense to me was returning a struct, or specifically an array of structs, but I do not think I applying it correctly here:
struct entry dictionary_sort(struct entry dictionary)
In total, my current version of the program is as follows:
#include <stdio.h>
#include <stdbool.h>
struct entry {
char word[15];
char definition[50];
};
// Function to compare two character strings
int compare_strings(const char s1[], const char s2[])
{
int i = 0, answer;
while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
i++;
if (s1[i] < s2[i])
answer = -1; // s1 < s2
else if (s1[i] == s2[i])
answer = 0; // s1 == s2
else
answer = 1; // s1 > s2
return answer;
}
// Function to sort a dictionary structure
struct entry dictionary_sort(struct entry dictionary[])
{
int dictionary_length = sizeof(dictionary) / sizeof(dictionary[0]);
int i, j, minimum;
struct entry temp;
for (i = 0; i < dictionary_length; i++) {
minimum = i;
for (j = i + 1; j < dictionary_length; j++) {
if (compare_strings(dictionary[j].word,
dictionary[minimum].definition) == -1)
minimum = j;
}
temp = dictionary[minimum];
dictionary[minimum] = dictionary[i];
dictionary[i] = dictionary[minimum];
}
return dictionary;
}
int main(void)
{
struct entry dictionary[10] =
{{"aardvark", "a burrowing African mammal"},
{"abyss", "a bottomless pit"},
{"addle", "to become confused"},
{"aerie", "a high nest"},
{"ajar", "partially opened"},
{"acumen", "mentally sharp; keen"},
{"affix", "to append; attach"},
{"agar", "a jelly made from seaweed"},
{"ahoy", "a nautical call of greeting"},
{"aigrette", "an ornamental cluster of feathers"}};
int i, dictionary_length = sizeof(dictionary) / sizeof(dictionary[0]);
dictionary = dictionary_sort(dictionary);
for (i = 0; i < dictionary_length; i++)
printf("%s - %s.\n", dictionary[i].word, dictionary[i].definition);
return 0;
}
The string comparison function behaves as expected since it is only returning an integer. I am really at a loss as to how to have the desired functionality without knowledge of pointers. There are enough examples with pointers to look up, but I am curious what fundamental principle I am missing here because I feel as though everything else in the book has come very naturally to me.
Thank you in advance!
n * k, wherekis the size of the struct. That's alla[i]means anyway.structarray at a time? It just seems inefficient to have to rearrange it for each call. I know this is a contrived example, but it seems like he is asking for a function that acts on the whole array at once.a[i], you are referring to that particular structure in the array. What you do with it at that point is entirely up to you. You can read it, write it, replace it with a different struct, etc. You can modify it in place.