AllAnimals - 1 year ago 60
C Question

# Binary tree implementation gives me segmentation fault in C

I'm getting a segmentation fault when trying to access data stored in my TreeNode. Here is the code:

``````#include <stdio.h>
#include <stdlib.h>

typedef struct NodeTag{
int value;
} TreeNode;

void inOrder(TreeNode * n){
printf("%d ", n->value);
}

void newNode(TreeNode * n, int v){
n = malloc(sizeof(TreeNode));
n->value = v;
}

void addValue(TreeNode * r, int value){

if(value < r->value){
} else {
}
} else if (value > r->value) {
} else {
}
}
}

int main(){

TreeNode * root = 0;
newNode(root, 1);
printf("%d\n", root->value); //<--This is where I get the fault

//inOrder(root);

return 0;
}
``````

If anyone can explain to me why I'm getting this error it would be greatly appreciated. I'm a student learning C and I'm not too familiar with pointers and such.

``````void newNode(TreeNode * n, int v){
n = malloc(sizeof(TreeNode));
n->value = v;
}
``````

In this code, `n` is a pointer to a `TreeNode` struct but if you assign something to `n`, this is not visible outside of the function as the pointer is passed by value.

``````void writeToA ( int a ) {
a = 5;
}

int main ( ) {
int x = 10;
writeToA(x)
printf("%d\n", x);
}
``````

What will this code print? It will print `10`, not `5`. That's because the value of `x` is passed to the function, not a reference to `x`. Changing that value within the function will not change the value of `x` outside the function.

A pointer is also a value, basically it is an int and the int value is a memory address:

``````void writeToPtr1 ( int * a ) {
int i = 10;
a = &i; // `a` now points to the memory address of i
}

void writeToPtr2 ( int * a ) {
*a = 5; // This doesn't change where `a` points to,
// it writes 5 to the memory address to that `a` points to.
}

int main ( ) {
int x = 10;
int *ptr = &x; // ptr now points to the memory address of x!
writeToPtr1(ptr);
// ptr still points to the memory address of x!
// As not a reference to ptr was passed, the memory
// address of x was passed to the function!

writeToPtr2(ptr);
// ptr still points to the memory address of x!
// But this memory now has the value 5 and not 10 anymore.
}
``````

You need to return the result of the allocation:

``````TreeNode * newNode ( int v ) {
TreeNode * n = malloc(sizeof(TreeNode));
n->value = v;
return n;
}

int main ( ) {
TreeNode * root = newNode(1);
printf("%d\n", root->value);
return 0;
}
``````

Or you need to pass a reference to the pointer and then change the value the pointer points to:

`````` void newNode ( TreeNode ** outNode, int v ) {
// TreeNode ** is a pointer to a pointer to a TreeNode!
TreeNode * n = malloc(sizeof(TreeNode));
n->value = v;