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 5:06

Okay now I got rid of the segmentation fault

Too bad the algorithm for the catalan number is wrong.

babby_face.jpg

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