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.
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.