1. ## quadtree structure segmentation fault

Hi this is my first post here and i'm fairly new to C programming.

So i'm trying to make a quadtree, and included in a header file is the node structure as follows:

Code:
``` typedef struct tnode *tree;

struct tnode
{
tree *P;
tree *NW;
tree *NE;
tree *SE;
tree *SW;
char colour;
int size;
int x;
int y;
};```
Also in the header file is my function declaration for making the quadtree:

Code:
`tree makequadtree(tree node, int array[MAXSIZE][MAXSIZE], int size);`
In my C file, I have called the function as follows:

Code:
`makequadtree(node, my_2D_array, size);`
However my quadtree function gives me a segmentation fault. I have pasted the relevant part of this function:

Code:
```tree makequadtree(tree node, int array[MAXSIZE][MAXSIZE], int s)

{
int q,w,a,b;
int NW_array[MAXSIZE][MAXSIZE]={0}, NE_array[MAXSIZE][MAXSIZE]={0};
int SE_array[MAXSIZE][MAXSIZE]={0}, SW_array[MAXSIZE][MAXSIZE]={0};

node->size = s;
node->NW = NULL;
node->NE = NULL;
node->SE = NULL;
node->SW = NULL;

if(get_colour(array, s) == 'G')
{

node->colour = 'G';

//NW BLOCK:
for(q=0,w=0, a=0,b=0; w<s/2, b<s/2; q++, a++)
{
NW_array[q][w] = array[a][b];
if(q==(s/2)-1)
{
q=-1;
a=-1;
w++;
b++;
}
}

//NE BLOCK:
for(q=0,w=0, a=2,b=0; w<s/2, b<s/2; q++, a++)
{
NE_array[q][w] = array[a][b];
if(q==(s/2)-1)
{
q=-1;
a=1;
w++;
b++;
}
}

//SE BLOCK:
for(q=0,w=0, a=2,b=2; w<s, b<s; q++, a++)
{
SE_array[q][w] = array[a][b];
if(q==(s/2)-1)
{
q=-1;
a=1;
w++;
b++;
}
}

//SW BLOCK:
for(q=0,w=0, a=0,b=2; w<s, b<s; q++, a++)
{
SW_array[q][w] = array[a][b];
if(q==(s/2)-1)
{
q=-1;
a=1;
w++;
b++;
}
}

}
else if(get_colour(array, s) == 'B')
{
node->colour = 'B';
}

else node->colour = 'W';

}```
The problem seems to lie in 'node->size = s;' and any other time I try to pass a value to a node. Perhaps I have defined my structure incorrectly?

Any help would be much appreciated!
Pedro.

2. Segmentation fault is an obvious result as you call makequadtree with NULL pointer.
Code:
```node->NW = NULL;
node->NE = NULL;
node->SE = NULL;
node->SW = NULL;
// ...
You declared tree as a pointer
typedef struct tnode *tree;
so these
tree *P;
tree *NW;
tree *NE;
tree *SE;
tree *SW;

are double pointers - is this what you intended?

3. Thanks for your reply. So they are not pointing anywhere? I intend to pass an initial node into makequadtree, and pass it values into its 'colour' and 'size' and then pass node->NW, node->NE, node->SE and node->SW recursively into makequadtree. How should I go about this?

4. I don't know if this is a stupid statement or not but I somehow expected more malloc's in this code. I'm fairly confident that you have to actually malloc the deeper branches of the tree, right? Or can you just statically allocate this to victory? Either way, something seems to be needed to be allocated.

5. Yes that's very true I should allocate memory to these nodes...I have done so now with
Code:
`node->NE = (tnode*) malloc(sizeof(tnode));`
to each node