c - Should not a local BST node created in a function be no, as function generally loose any data created in it? -


i have been using functions create binary search trees, stuck question:

functions not allow local variable accessed outside itself. in case of function creating struct node , returning address saved further , being accessed main.

isn't local variable others. can't find missing. please help.

edit:

being language specific, use c , return address in case of struct.

you need return pointer, this:

node_t* node_create() {     node_t* node = calloc(1, sizeof(node_t));     /* maybe populate actual data in node */     return node; }  void node_destroy(node_t* node) {     free(node); } 

by using calloc or malloc, can "dynamic" memory allocation, , lets return structs such can link them tree or whatever , not have them go out of scope.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -