1

エラーが発生せずに factorial() と呼ばれるこの関数を実行できないようです。

最初に があればinbuf = atoi(factorial(inbuf));、gcc が吐き出しますが、

main.c:103: warning: passing argument 1 of ‘factorial’ makes integer from pointer without a cast

に変更するとinbuf = atoi(factorial(inbuf*));、gccが吐き出し、

main.c:103: error: expected expression before ‘)’ token

関連コード:

int factorial(int n)
{
    int temp;

    if (n <= 1)
        return 1;
    else 
        return temp = n * factorial(n - 1);
} // end factorial

int main (int argc, char *argv[])
{
    char *inbuf[MSGSIZE];
    int fd[2];

    # pipe() code
    # fork() code

    // read the number to factorialize from the pipe
    read(fd[0], inbuf, MSGSIZE);

    // close read
    close(fd[0]);

    // find factorial using input from pipe, convert to string
    inbuf = atoi(factorial(inbuf*));

    // send the number read from the pipe to the recursive factorial() function
    write(fd[1], inbuf, MSGSIZE);

    # more code

} // end main

逆参照と私の構文について何が欠けていますか??

4

2 に答える 2

2

この回線でコールを再配置する必要があります。

inbuf = atoi(factorial(inbuf*));

する必要があります

int answ = factorial(atoi(inbuf));

char *inbuf[MSGSIZE];*他のすべてのコードが機能すると仮定しますが、inbuf の宣言を からに変更する必要があると思いますchar inbuf[MSGSIZE];

于 2011-04-12T19:51:37.573 に答える
1

まず、inbufを次のように変更します。char inbuf[MSGSIZE];

次に、 inbufintに変換して に渡す必要がありますfactorial()atoi()まさにそれを行います。次に、この操作の結果を取得して文字列に変換し、inbufに割り当てます。これが理由sprintf()です。

// find factorial using input from pipe, convert to string
sprintf(inbuf, "%d", factorial(atoi(inbuf)));
于 2011-04-12T20:10:27.087 に答える