I'm a beginner in C programming. I am currently trying to implement a special tree using an array. When executing my code it stops saying: Process finished with exit code 11 (Segmentation fault). For some reason it stops before entering my function newTree. At the moment, the only thing that my program does is: it first displays the menu and then asks the user for a choice. After that it asks the user to enter the root name and value. For some reason it then develops a segmentation fault.
This is my current code:
//struct
typedef struct Tree {
char *name;
char *value;
_Bool isLeaf;
} Node;
//array
Node *tree;
//defining my functions
void newTree(Node n);
void add_child(Node n1, Node n2, int child_loc);
int main(void){
//menu
printf("The Tree Data Structure\n");
printf("Choose one of the following options: \n");
printf("1.Create a new tree consisting just of the root node n \n");
printf("2.Add a new child node to n \n");
printf("3.Prune \n");
printf("4.List Children \n");
printf("5.Traverse \n");
printf("6.Graft \n");
printf("7.Search \n");
printf("Option: ");
Node n, n1, n2;
//creating all nodes
for(int i=0; i<781; i++){
Node i = {(char*)malloc(sizeof(char)*16), (char*)malloc(sizeof(char)*80), 0};
}
int option;
scanf("%d", &option);
switch(option){
case 1:
printf("Enter root name: ");
scanf("%s", n.name);
printf("Enter root value: ");
scanf("%s", n.value);
//my problem
newTree(n);
break;
//other cases
default: printf("Incorrect option");
break;
}
return 0;
}
void newTree(Node n) {
strcpy(tree[0].name,n.name);
strcpy(tree[0].value,n.value);
int first_child_loc;
first_child_loc = 0;
//0 because first tree: tree[0]
first_child_loc = (0*5)+1;
if(strcmp("",tree[first_child_loc].name) == 0){
n.isLeaf = 1;
}else{
n.isLeaf = 0;
}
tree[0].isLeaf = n.isLeaf;
}
Thank you very much.