ライブラリとして提供される一般的な要素セットがあります。C:関数ポインタを渡すときの一般的なADTエラー
/** Type for defining the set */
typedef struct Set_t *Set;
/** Element data type for set container */
typedef void* SetElement;
/** Type of function for copying an element of the set */
typedef SetElement(*copySetElements)(SetElement);
セットを作成するには、セットを使用する要素のコピーを処理する関数へのポインタを指定する必要があります。
Set setCreate(copySetElements copyElement);
私は以下のタイプやコピー機能を書いた:
typedef struct location_t {
char *name;
} *Location;
Location locationCopy(Location location){
Location new_location = locationCreate(location->name);
return new_location;
}
*もちろん、私は議論の焦点を合わせるためにすべてを簡素化。
は私が呼ぶとき:
Set locations = setCreate(locationCopy);
私はコンパイルエラーを取得:
警告:互換性のないポインタ タイプから 'setCreate' の引数1を渡し
期待 'copySetElements' は引数の型は 'struct location_t *(*)(struct location_t *)'です。
おそらく関数ポインタを除いて、 'typedef'の後ろにポインタ性質を隠さないでください。あなたを含む誰もが混乱します。 –