Search code examples
cgcccastingdereference

Can't derefence variable so function is satisfied


I can't seem to run this function called factorial() without getting an error.

At first if I have inbuf = atoi(factorial(inbuf));, gcc will spit out,

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

If I change it to inbuf = atoi(factorial(inbuf*));, gcc will spit out,

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

Relevant code:

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

What am I missing about dereferencing and my syntax??


Solution

  • You need to rearrange your calls on this line:

    inbuf = atoi(factorial(inbuf*));
    

    should be

    int answ = factorial(atoi(inbuf));
    

    *Assuming all the other code works, but I think you need to change the declaration of inbuf from char *inbuf[MSGSIZE]; to char inbuf[MSGSIZE];