1

私は StackOverflow を初めて使用し、なぜ私の C コードでこのエラーが発生するのか疑問に思っています。私は本当にこれを解決したいと思っています。誰かがなぜこれが起こっているのかを説明できれば、答えを教えていただければ幸いです.

void scanningForWS(int argc, char **argv)
{

int number = 0;
int sflag = 0;
int opt = 0;
int *s = 0;
char *getopt = 0;
char *optarg = 0;

while ((opt = *getopt(argc, argv, "w:s")) != -1) //the *getopt gives me this error
//Error: Expression much have a pointer-to function

{
switch (opt)
{
case 's':
    sflag = 1;
    break;
case 'w':
    number = atoi(optarg);
    break;
default:
    break;
}
}

}

それは while 文です。必要に応じてコメントしました。

問題が見つかりましたが、まだ解決されていません。unistd.h がなく、取得できないことがわかりました。どこで入手できるか知っている人はいますか?

4

3 に答える 3

1

関数と同じ名前の変数を宣言していますが、適切なヘッダーが含まれているかどうかはわかりません。

これは関数宣言ではありません。参考までに、その行は char へのポインターを宣言し、値で初期化します0。現在のコードが意味をなす唯一の方法は、ifgetoptが関数ポインタであり、そうではない場合です。

コードは次のようになります。

#include <unistd.h>

void scanningForWS(int argc, char **argv)
{
    int number = 0;
    int sflag = 0;
    int opt = 0;
    int *s = 0;    
    /* char *getopt = 0; do not declare getopt as a variable, 
                         just include the header uninstd.h and use it */

    while ((opt = getopt(argc, argv, "w:s")) != -1)
        /* ... */
}
于 2012-12-07T18:59:50.227 に答える
1

Removechar *getopt; getoptはからの関数でunistd.hあり、その char ポインターを宣言することにより、非常に奇妙なことをしています:)

于 2012-12-07T19:04:02.433 に答える
0

getopt はintではなく を返しますint *

*use in while ループから を削除します。

while ((opt = getopt(argc, argv, "w:s")) != -1) //the *getopt gives me this error

に:

while ((opt = getopt(argc, argv, "w:s")) != -1) //the *getopt gives me this error
于 2012-12-07T18:56:36.910 に答える