Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struct in Struct : How to reference each member : & Allocate memory 1

Status
Not open for further replies.

AnnaHere

IS-IT--Management
May 16, 2000
17
0
0
AU
Hello Gentlemen, Ladies,<br><br>Not sure if you can help me ?<br>I am trying to reference items in a structure within a structure. And also allocating memory with &quot;malloc&quot;.<br><br>Please see I have created a Structure (word_entry) , and then created another Structure (Node) which contains previous structure.<br><br>The reason I have done this is because I will be reading string of characters of various lengths and allocating memory dynamically to each String depending on length.<br>Rather than just make every word sit in a fixed length array even when one word may be only 2 chars in length.<br>-------------------------------------------------------- <br>struct word_Entry {<br> char *word&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* String Word entry */<br>};<br><br>struct Node {<br> struct Node *leftPtr;<br> struct word_Entry *entry;<br> struct Node *rightPtr;<br>};<br><br>typedef struct Node TREENODE;<br>typedef TREENODE *TREENODEPTR;<br><br>void insertNode (TREENODEPTR *treePtr, char *dictentry)<br>--------------------------------------------------------<br>Question (1)<br>============<br>How do I allocate memory to each structure dynamically<br>based on &quot;String&quot; length. (in function &quot;insertNode&quot;)<br><br>void insertNode (TREENODEPTR *treePtr, char *dictentry)<br>{<br>...<br>...<br><br>*treePtr = (TREENODEPTR)malloc(sizeof((char)* strlen ??????;<br><br>...<br>...<br>}<br><br>-------------------------------------------------------<br>Question(2)<br>==========<br>How do I copy a &quot;String&quot; into the Structure<br>strcpy( (*treePtr).??????? , dictentry );<br>
 
Hi Anna,<br>&nbsp;&nbsp;First of all I didnt get why you need to use a separate structure for storing words, instead of that you can simplify the tree structure by creating a structure like this.<br>&nbsp;&nbsp;struct treenode {<br>&nbsp;&nbsp;struct treenode *left, *right;<br>&nbsp;&nbsp;char *word ;<br>&nbsp;&nbsp;};<br>&nbsp;&nbsp;typedef struct treenode TREENODE;<br>&nbsp;&nbsp;typedef TREENODE* TREENODEPTR;<br><br>&nbsp;&nbsp;Coming to allocation of memory, I think this shuld do good, treePtr = (TREENODEPTR) malloc(sizeof(TREENODE) + strlen(buf));<br>&nbsp;&nbsp;I never tried, but u can try these.<br><br>Regards,<br>Baskar.
 
Hello Baskar,<br><br>I have too many strings to store into memory , and all vary in length.<br>I will run out of memory, unless I allocate memory dynamically individual to each string size.<br>i.e Some strings contain 2 char, some 32 chars<br>Therefore I want to allocate memory for 2 chars only, and 32 chars only, ...etc.....<br>I have a problem..<br>Anna<br><br><br>I think I will have to do something like this, every time I insert &quot;String&quot; into node :-<br>*treePtr = (TREENODEPTR)malloc(sizeof(char)* strlen(??????))<br><br><br><br><br>
 
What baskar says is also right and in any case if you want to use your own way then also you can do that. Just do like this way:<br><br>TREENODEPTR&nbsp;&nbsp;treePtr;<br>treePtr = (TREENODEPTR) malloc(sizeof(TREENODE));<br><br>Will allocate memory to treePtr. Then when you are going to store into treePtr(binary tree) . You can allocate memory dynamically to word like:<br><br>treePtr-&gt;entry-&gt;word = ( char*) malloc(sizeof(char) * strlen(&quot;helloworld));<br><br>This way you can allocate memory for string to any node. No need to allocate memory to whole node. Just assign only to word which is going to take the string value.<br><br>Does this answer your question ?<br>Thanx<br>Siddhartha Singh<br>&nbsp;&nbsp;<br><br>struct word_Entry {<br>char *word&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* String Word entry */<br>};<br><br>struct Node {<br>struct Node *leftPtr;<br>struct word_Entry *entry;<br>struct Node *rightPtr;<br>};<br><br>typedef struct Node TREENODE;<br>typedef TREENODE *TREENODEPTR;<br><br> <p>Siddhartha Singh<br><a href=mailto:siddhu_singh@hotmail.com>siddhu_singh@hotmail.com</a><br><a href=siddhu.freehosting.net> </a><br>
 
Dear AnnaHere,<br><br>Here is one approach that I consider appropriate to your question:<br><br>struct Node<br>{<br>&nbsp;&nbsp;Node* _left;<br>&nbsp;&nbsp;Node* _right;<br>&nbsp;&nbsp;char* word;<br>};<br><br>typedef struct Node TREENODE;<br>typedef TREENODE *TREENODEPTR;<br><br>TREENODEPTR Node_create( Node* left, Node* right, char* dictEntry){<br><br>&nbsp;&nbsp;TREENODEPTR pNode = (TREENODEPTR)malloc( sizeof(TREENODE));<br>&nbsp;&nbsp;memset( pNode, 0, sizeof(TREENODE));<br>&nbsp;&nbsp;pNode-&gt;_left = left;<br>&nbsp;&nbsp;pNode-&gt;_right = right;<br><br>&nbsp;&nbsp;if ( dictEntry){<br>&nbsp;&nbsp;&nbsp;&nbsp;// copy string<br>&nbsp;&nbsp;&nbsp;&nbsp;pNode-&gt;word = (char*)malloc(sizeof(char) * strlen(dictEntry));<br>&nbsp;&nbsp;&nbsp;&nbsp;strcpy( pNode-&gt;word, dictEntry);<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;return pNode;<br>}<br><br>Then if you need to you could do things like this:<br><br>void Node_setLeft(Node* pthis, Node* pLeft){<br>&nbsp;&nbsp;pthis-&gt;_left = pLeft;<br>}<br><br>Node* Node_getLeft(Node* pthis){<br>&nbsp;&nbsp;return pthis-&gt;_left;<br>}<br><br>&quot;but that's just my opinion... I could be wrong&quot;.<br>-pete
 
Guys Thank you for your help<br>palbano, Basking, ssingh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top