1

typedef void (callback)(int *p1, sStruct *p2);

typedef struct _sStruct
{
callback *funct;
}sStruct;

C で次の宣言をしています。エラーを受信せずにこの繰り返し宣言をコンパイルするにはどうすればよいですか?

現時点では、最初の行の「*」トークンの前に構文エラーが発生しました。

4

1 に答える 1

11

構造を前方宣言できます。

/* Tell the compiler that there will be a struct called _sStruct */
struct _sStruct;

/* Use the full name "struct _sStruct" instead of the typedef'ed name
   "sStruct", since the typedef hasn't occurred yet */
typedef void (callback)(int *p1, struct _sStruct *p2);

/* Now actually define and typedef the structure */
typedef struct _sStruct
{
  callback *funct;
} sStruct;

編集:質問のタイプ名の変更に合わせて更新されました。

また、構造体に identifier を指定しないことを強くお勧めします_sStruct。a で始まるグローバル名_は予約名であり、それらを独自の識別子に使用すると、未定義の動作が発生する可能性があります。

于 2010-07-28T16:38:27.500 に答える