Thread: Help to run this problem.

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    17

    Help to run this problem.

    So I was trying to pass the address to a function and I'm getting some error I think which is not related to passing the address but with the function or something. Here:
    Code:
    #include <stdio.h>
    
    int fac(int *px)//prototype
    
    
    //start of main
    int main(){
        int x;//stores the input
        scanf("%d", &x);//input
        int factorial;//stores factorial
        factorial = fac(&x);//passing the address/pointer to x
        printf("%d", factorial);//printing the factorial
        return 0;
    }// end of main
    
    
    //funtion fac calculates factorial
    
    
    //fac starts
    int fac(int *px){
        int factorial = 1;// stores result
    
    
        //start of while/calculating factorial
        while(*px!=0){
          factorial *= *px;
          *px--;
        }//while ends
    
    
        return factorial;//returns calculated result
    }//fac ends

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,347
    Note that *px-- is equivalent to *(px--) rather than (*px)--

    That said, why not pass a copy of the int rather than a pointer to it? You're not using the modified value of the variable after the function call anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    17
    Thanks for the help but I am still getting the same error, here:
    Code:
    main.c: In function ‘fac’:
    main.c:7:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
     int main(){
               ^
    main.c:21:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
     int fac(int *px){
                     ^
    main.c:33:1: error: expected ‘{’ at end of input
     }//fac ends
     ^

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,347
    You forgot the semi-colon at the end of the function prototype.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2019
    Posts
    17
    Oh how naive I am

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread