Thread: Problems using pointers, i think

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    11

    Unhappy Problems using pointers, i think

    guys,

    i'm newbie in C, them i'm trying to write a C code that calculates the fatorial, but when i'll compile the source i got this stack >

    Code:
    main.c: In function ‘fatorial’:
    main.c:5:13: warning: comparison between pointer and integer
    main.c:8:23: error: invalid operands to binary * (have ‘int **’ and ‘int **’)
    I'm trying to use two methods, and call fatorial in main, follow the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void fatorial(int *number){
        if(&number == 1){
            return;
        }
        fatorial((&number-1) * &number);
    }
    
    float main(){
        int fat = 0;
        scanf("%d", &fat);
        fatorial(&fat);
        printf("%d", fat);
    }
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Just repeating my remarks because I think you missed the last two or more of my edits.

    FYI: Most basic recursion functions do NOT require pointers to solve/implement.
    FYI: Most basic recursion functions return a value.
    Note: Factorial value of zero is the one. I think you code will have issues if passed zero as input.
    Note: I think you code is likely to only work if passed the value of one.

    Tim S.
    Last edited by stahta01; 04-19-2012 at 11:22 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    I've changed my code, and i got this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void fatorial(int *number){
        if((*number) == 1){
            return;
        }
        fatorial(((*number)-1) * (*number));
    }
    
    float main(){
        int fat = 0;
        scanf("%d", &fat);
        fatorial(&fat);
        printf("%d", fat);
    }
    it compiles, it runs, but when i pass the parameters (just one) and type enter it prints "Segmentation fault", some idea, how to fix this one?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Decided this post help the user too much; so deleted content.
    Last edited by stahta01; 04-19-2012 at 11:30 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I get several compile errors when I try to compile your code:
    main.c||In function ‘fatorial’:|
    main.c|8|error: passing argument 1 of ‘fatorial’ makes pointer from integer without a cast|
    main.c|4|note: expected ‘int *’ but argument is of type ‘int’|
    main.c|11|error: return type of ‘main’ is not ‘int’|
    main.c||In function ‘main’:|
    main.c|16|warning: control reaches end of non-void function|
    ||=== Build finished: 3 errors, 2 warnings ===|
    Jim

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    float main should be int main. I suggest that you implement the factorial function in this incomplete program:
    Code:
    #include <stdio.h>
    
    int factorial(int number)
    {
        /* complete this */
    }
    
    int main(void)
    {
        int result = factorial(4);
        printf("%d\n", result);
        return 0;
    }
    When you have this down pat, then you can start thinking of what happens if you want to use a pointer parameter instead and change the return type to void.
    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

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    fatorial(((*number)-1) * (*number)) That takes an integer not a pointer to an integer.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    I'm using gcc compiler, and i got this statement when i compiled the code.

    Quote Originally Posted by jimblumberg View Post
    I get several compile errors when I try to compile your code:

    Code:
    main.c:8:2: warning: passing argument 1 of ‘fatorial’ makes pointer from integer without a cast
    main.c:4:6: note: expected ‘int *’ but argument is of type ‘int’
    Jim

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    guisantogui: accept my challenge in post #6. Until you prove that you can handle that, you have a snowball's chance in hell of getting this right.
    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

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Ok, let's do it, now!
    Quote Originally Posted by laserlight View Post
    guisantogui: accept my challenge in post #6. Until you prove that you can handle that, you have a snowball's chance in hell of getting this right.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    HAHA, it was easy

    Code:
    #include <stdio.h>
    
    int factorial(int number){
       if(number == 1){
           return number;
       }
       return factorial(number-1) * number;
    }
    
    int main(){
        int result = factorial(4);
        printf("%d\n", result);
        return 0;
    }
    But, i was trying to use pointers, to learn about that, do you have some "challenge" using pointers laserlight?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Very good - now count how many temporary integers are created when you calculate factorial(4)

    Now think how many integers exist when you try it with your int* approach.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    Ok, here we go!
    Quote Originally Posted by Salem View Post
    Very good - now count how many temporary integers are created when you calculate factorial(4)

    Now think how many integers exist when you try it with your int* approach.

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    11
    It was harder than another challenge, but i got this (i think), follow the code:

    Code:
    #include <stdio.h>
    
    int factorial(int number, int *cont){
        *cont = *cont+1;
        if(number == 1){
            return number;
        }
        //printf("%d",cont);
        return factorial(number-1, cont) * number;
    }
    
    int main(){
        int cont = 0;
        int val;
        scanf("%d", &val);
        int result = factorial(val, &cont);
        printf("%d\n%d\n", result, cont);
        return 0;
    }
    Quote Originally Posted by Salem View Post
    Very good - now count how many temporary integers are created when you calculate factorial(4)

    Now think how many integers exist when you try it with your int* approach.
    Then, do you have another challenge?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by guisantogui
    Then, do you have another challenge?
    If you are taking this as a challenge, then you did not actually meet the challenge

    So, complete the factorial function of this program without changing the function's signature or return type:
    Code:
    #include <stdio.h>
    
    void factorial(int *number)
    {
        /* complete this */
    }
    
    int main(void)
    {
        int result = 4;
        factorial(&result);
        printf("%d\n", result);
        return 0;
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new to pointers having problems
    By traxy in forum C Programming
    Replies: 2
    Last Post: 05-18-2008, 02:32 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. problems with pointers
    By Mahesh in forum C Programming
    Replies: 7
    Last Post: 03-29-2005, 08:13 PM
  4. Problems with pointers
    By Pea in forum C Programming
    Replies: 9
    Last Post: 11-30-2004, 03:01 AM
  5. Problems with pointers
    By rhildebrand in forum C Programming
    Replies: 2
    Last Post: 10-29-2003, 07:57 PM

Tags for this Thread