无法取消引用变量以使功能得到满足

| 我似乎无法在没有错误的情况下运行名为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
我缺少有关取消引用和语法的信息吗?     
已邀请:
您需要在此线路上重新安排呼叫:
inbuf = atoi(factorial(inbuf*));
应该
int answ = factorial(atoi(inbuf));
*假设所有其他代码都可以,但是我认为您需要将inbuf的声明从
char *inbuf[MSGSIZE];
更改为
char inbuf[MSGSIZE];
    
首先,将inbuf更改为:
char inbuf[MSGSIZE];
其次,您需要将inbuf转换为int才能将其传递给
factorial()
atoi()
正是这样做的。然后,获取该操作的结果,并将其转换回字符串,然后将其分配给inbuf:这就是
sprintf()
这样做的原因。
// find factorial using input from pipe, convert to string
sprintf(inbuf, \"%d\", factorial(atoi(inbuf)));
    

要回复问题请先登录注册