i' write function pass words , values bst
struct array. in tree
have words(node->word)
, value(node->val)
.
in main
declare array of pair struct.
here code:
void inorder(tree *node,pair *array[], int index) { if(node == null){ // recursion anchor: when node null empty leaf reached (doesn't matter if left or right, end method call return; } inorder(node->left, array, index); // first every left child tree array[index]->val= node->val; // write data in array array[index]->word = malloc(sizeof(char)*(strlen(node->word)+1)); strcpy(array[index]->word,node->word); index++; inorder(node->right, array, index); // same right child } int main(int argc, char *argv[]) { tree *mytree = null; pair arr[5000]; int index=0; ... inorder(mytree,&arr,index); printf("%d",arr[0].val); zero(mytree); return 0; }
debugger says:
access violation writting location 0x0000001.
something weird pointers here. inorder
function header expects array of pair
pointers, pass in pointer array of pair
s (which chunk of random memory). i'm pretty sure that's pointer error comes from.
there many ways fix this, i'll put 1 best. reason why you're passing pointer pointer instead of pointer? try changing function header:
void inorder(tree *node, pair *array, int index)
and access stuff this:
array[index].val= node->val; // write data in array array[index].word = malloc(sizeof(char)*(strlen(node->word)+1)); strcpy(array[index].word,node->word);
and call main
this:
inorder(mytree,arr,index);
i can't test it, unfortunately, think should work.
p.s. sorry edits/deletions. misread something.
Comments
Post a Comment