An exercise at the end of chapter 10 of "Programming in C" by Stephen Kochan says to write a function that sorts an array of structs
in alphabetical order.
The struct has the form
struct entry
{
char word[15];
char definition[50];
};
which mimics a dictionary. An array of these structs
would look like this
const struct entry dictionary[10] =
{
{"agar", "a jelly made from seaweed"},
...,
...,
{"aerie", "a high nest"}
}
The definition of struct entry
is global, dictionary
is in main.
I wrote a function that is supposed to sort this dictionary alphabetically called dictionarySort
void dictionarySort(struct entry dictionary[], int entries)
with entries
being the number of elements in dictionary
. In main
I declare the function and call it with
dictionarySort(dictionary, 10);
Now I get the errors
warning: passing argument 1 of ‘dictionarySort’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
for the function call and
note: expected ‘struct entry *’ but argument is of type ‘const struct entry *’
void dictionarySort(struct entry dictionary[], int entries)
for the function header.
I found Passing an array of structs in C and followed the accepted answer but it still does not work. Note that I have not learned about pointers as they have not been introduced in the book yet.