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:36

YOU HAVE TWO OPTIONS:

1)
int *n;
int *result;
scanf("%d",n);
*result = catalan(*n)*catalan(*n-1);
printf("Catamarangatang number: %d\n", *result);


2)
OR YOU CAN JUST REMOVE THE MALLOC LINES BECAUSE YOU DON'T EVEN FUCKING HAVE ANY POINTERS AND YOU DON'T FUCKING NEED ANY ``pudding''

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