Stephen Kochanの "Programming in C"の第10章の最後の演習では、structs
の配列をアルファベット順にソートする関数を記述しています。構造体の配列を関数に渡す
構造体は、辞書を模倣形態
struct entry
{
char word[15];
char definition[50];
};
を有しています。これらstructs
の配列は、この
const struct entry dictionary[10] =
{
{"agar", "a jelly made from seaweed"},
...,
...,
{"aerie", "a high nest"}
}
ようになりstruct entry
の定義はdictionary
がメインであり、グローバルです。
私はentries
はdictionary
内の要素の数であるとアルファベット順にdictionarySort
void dictionarySort(struct entry dictionary[], int entries)
と呼ばれるこの辞書を並べ替えることになっている機能を書きました。 main
では、私は関数を宣言し、
dictionarySort(dictionary, 10);
でそれを呼び出す今、私は取得エラー関数の
warning: passing argument 1 of ‘dictionarySort’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
と
note: expected ‘struct entry *’ but argument is of type ‘const struct entry *’ void dictionarySort(struct entry dictionary[], int entries)
ヘッダ。
私はPassing an array of structs in Cを見つけて、受け入れられた答えに従いましたが、それでも動作しません。私はまだ彼らが本に紹介されていないので、私はポインタについて学んでいないことに注意してください。
この回答は普遍的ではありません。この場合正しいですが、多くの場合、正しい答えは "関数に' const'を追加することです。可能であれば、それを好むべきです。 –
それは、ヘッダーが 'const'であると定義しているので、関数内で渡された引数を変更できないということではありませんか? – jruota