Problems using pointers, i think

This is a discussion on Problems using pointers, i think within the C Programming forums, part of the General Programming Boards category; guys, i'm newbie in C, them i'm trying to write a C code that calculates the fatorial, but when i'll ...

  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
    2,768
    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 12:22 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    2,768
    Decided this post help the user too much; so deleted content.
    Last edited by stahta01; 04-19-2012 at 12:30 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    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
    22,270
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    643
    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
    22,270
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    32,824
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    22,270
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 03: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21