0

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.

lfarr
  • 17
  • 6
  • You might be interested in this discussion (which is not directly related to your problem) http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc. While I cant find a function called `createTree`I assume you meant `newTree`. I assume your code prints `Enter root value: `. Be a bit more specific what your intended behaviour is and what the actual behaviour is. – Kami Kaze Dec 13 '16 at 12:12
  • 1
    Node *tree in the program just declares "tree" as a pointer. You will have to allocate memory to tree, to make it an array. Plus, I did not understand one thing, by making an array and storing in values, does not make it a tree. There has to be some link between each node in a tree. – mahesh Rao Dec 13 '16 at 12:14

3 Answers3

1

In newTree you access tree[0] in the strcpy call, but you never allocated memory for it. tree is just a global pointer, you could declare it as an array or if you need to change the size at runtime you could use malloc.

Kami Kaze
  • 2,069
  • 15
  • 27
1

You have provided a pointer "tree" and not an array "tree". You have to allocate enough memory to the pointer "tree" , to make it an array. You can do this by adding the following code

tree=(Node*)malloc(size_of_array*sizeof(Node));

This will form an array of "size_of_array" number of elements.
Also declare i in the for loop, but i don't think the loop will be required after allocating memory as said before.

mahesh Rao
  • 375
  • 1
  • 4
  • 16
0
  //creating all nodes
  for(int i=0; i<781; i++){
     Node i = {(char*)malloc(sizeof(char)*16),   (char*)malloc(sizeof(char)*80), 0};

  }

I haven't checked your whole program, but this for loop won't work for sure. you have an int i so i is already declared and initialized. Now you say that i is from type Node, this will result in an error. Try something like:

Node tree[781];
for(int i=0; i<781; i++){
     tree[i].name = (char*)malloc(sizeof(char)*16);
     tree[i].value = (char*)malloc(sizeof(char)*80);
}

Not tested yet.

izlin
  • 2,129
  • 24
  • 30
  • i had done something similar to that, but for some reason it only showed me "Enter root name ". With the previous version, although it makes no sense, it allowed me to enter a value after "Enter root value". – lfarr Dec 13 '16 at 12:28