Hi, help please!

I have a struct containing an array of pointers
to structs containing Binary Search Trees
with nodes containing structs.

... <*sigh*> I hate C soo much... put work wants it to be in C.

I can set the struct of BST's just fine, and keep it around, and I can set the innermost struct values. My problem is keeping those innermost values once I'm outside the function that puts them there.

void function(Struct struct, int i, int k)
    Array array;
    array = InitArray();
    array->key = k;
    struct->array[i] = &array;
void otherFunction(Struct struct, int i, int key, DStruct data)
    Tree tree;
    tree = InitTree(key, data);

    (*struct->array[i])->bst = tree;  //works, but only keeps value inside current function

    //(*struct->array[i])->bst = &tree; //incompatible pointer type... and Seg fault as soon as output is run
    PrintInorderTreeWalk((*struct->array[i])->bst);  //works, but it's only one node long

void anotherFunction(Struct struct, int i)
    if(struct->array[i]) != NULL)    //works
        if((*struct->array[i])->bst != NULL)   //Segmentation Fault for 'working' assignment above.
            PrintInorderTreeWalk((*struct->array[i])->bst);    //seg fault if the if stmt is commented out. 

I want that commented out command to actually, but I'm not good enough with the pointers to get it to.
And I realize I'm not giving a whole lot of code here, but I think it gets the important parts across. I'm not really allowed to put much of it out anyway, but I could change everything to struct A, B, C, etc if needed. It's just huge.

Thanks for the help!

5 Years
Discussion Span
Last Post by Ancient Dragon

You should pass the structures by reference, not by value. That means passing a pointer to the structure. I don't know what InitArray() does or returns, but I assume it returns a pointer. Here is how you need to write the first function you posted. Do something similar with the other functions.

Do not use reserved words as variable names. struct is a reserved word.

void function(Struct* str, int i, int k)
    str->array[i] = InitArray();
    str->array[i]->key = k;

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.