SourceOP
CPP Problem : - Printable Version

+- SourceOP (http://forums.sourceop.com)
+-- Forum: Half-Life 2 (http://forums.sourceop.com/Half-Life-2-forum)
+--- Forum: General Chat (http://forums.sourceop.com/General-Chat--24-forum)
+---- Forum: Coding (http://forums.sourceop.com/Coding-forum)
+---- Thread: CPP Problem : (/CPP-Problem-thread)



CPP Problem : - Banksy - Dec 14, 2009

hey, i have to write a recursive function that computes the factorial of a number given the following prototype:

Code:
void factorial (long argument, long *result)


here's what i have written, it compiles, but whenever i run it i get a segmentation fault. i'm pretty sure i'm just not using the pointer properly, any help would be greatly appreciated.

Code:
#include <stdio>

void factorial(long argument, long *result);

int main(){
    long a;
    long *result;

    *result = 1;
    
        printf("Enter a number: ");
        scanf("%l", &a);

    factorial(a, result);

        return 0;
}

void factorial(long argument, long *result){
    if (argument == 1)
        printf("The factorial of %l is: %l", argument,*result);
    else{
        *result = *result * argument;
        factorial(argument-1, result);    
    }
}


i'm pretty new to C so if there are any other obvious flaws feel free to point them out.


CPP Problem : - Drunken F00l - Dec 14, 2009

Banksy Wrote:
Code:
    long *result;

    *result = 1;

Stopped reading right there. (Read: There's your obvious flaw.)


CPP Problem : - OmegaZero_Alpha - Dec 14, 2009

long result is loooooooooooooooooooooooooooooooooooooooooooong