Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Segmentation faults ahoy!

Name: Anonymous 2010-12-01 4:29

Hey /prog/, total C noob here.
I wanted to write a recursive C function but somehow I always get a segmentation fault. This is my code:

########################################################
#include <stdio.h>
#include <stdlib.h>

main(){
   
    int n;                //depth of recursion
    printf("Enter number: ");
    scanf("%d", &n);
   
    int result;
    n = (int)malloc(sizeof(int));
    result = (int)malloc(sizeof(int));

    result = (catalan(n)*catalan(n-1));

    printf("Catalan number: %d \n", result);
   
    return result;

}

int catalan(int n){
    if(n==1){
        return 1;
    }
    return n*catalan(n-1);
}
########################################################

I've already tried some stuff with the memory allocation but it simply won't run.

Name: Anonymous 2010-12-01 4:43

>>7
Is there another way for compiling recursive functions?
Translating them into iterative functions, perhaps? (I think this is difficult, but maybe there are compilers that do this thing)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List