Question: I am trying to read a binary tree into my program using the following function that i wrote.

```
void readTree(struct Dnode *root)
{
    struct Dnode *temp;
    struct Dnode *input;
    char initData;

    scanf(" %c",&initData);
    input=CreateNode(initData);

    if(initData==122){
        printf("Leaf\n");
        return;
    }
    else {
        root=input;
        temp=root;
        printf("going to left of %c\n",initData);
        readTree(root->lLink);
        printf("going to right of %c\n",initData);
        readTree(root->rLink);
    }
}

```

Everything is working fine except the fact that , in main the tree doesnt exist. Here is my main program:

```
int main()
{
    int n=0;
    struct Dnode *root=NULL;
    printf("Enter the tree as it asks. Enter z whenever there is no children :\n");
    readTree(root);
    printf("%c",root->data);
    printf("In main");
    return;

 }

```

The program suddenly crashes after inputting the tree.Please help me.

Answer: 
Yes, there is a way to do that.

```
//Array for the functions
std::array<std::function<void()>, 3> functions = { &function1, &function2, &function3 };

//You could also just set them manually
functions[0] = &function1;
functions[1] = &function2;
functions[2] = &function3;

```

Then you can use it as a normal array:

```
functions[whichFunction](); //Calls function number 'whichFunction'

```

Do note that the functions all have to have the same signature.

---

If you do not want to use `std::function` for some reason, you can use [function pointers](https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work).
```
switch(whichFunction) {
    case 1: function1(); break;
    case 2: function2(); break;
    case 3: function3(); break;
}

```