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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing structure components as arguments to a routine

Status
Not open for further replies.

sirbu

Programmer
Sep 15, 1999
15
0
0
RO
If i have a structure like :<br><br>struct node {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double x,y;<br> &nbsp;&nbsp;&nbsp;&nbsp;} *n;<br><br>i would like to code a routine, called from main program, who reads the two components of the structure.<br><br>void ReadStruct( ? ? ? ? ? ? ?);<br><br>void main (void)<br>{<br> <br> ReadStruct( ? ? ? ? ?);<br>}<br><br>void ReadStruct( ? ? ? ? ?)<br>{<br>? ? ? ? ? ? ?<br>}<br><br>The problem is that i don't know how to write routine's header (maibe the routine itself) and how to pass arguments to the routine. Thank you in advance for help.<br>
 
Is there any particular reason you named the structure variable *n?&nbsp;&nbsp;I suspect you intend to use n as an array of x,y pairs, so why not just declare it?<br><br>Unless you are doing something esoteric, you don't need to &quot;read&quot; the structure, just use pointers:<FONT FACE=monospace><font color=red><b>q = n(i)-&gt;x</b></font></font>, or<FONT FACE=monospace><font color=red><b>q = n(i).x</b></font></font>,depending on how the declaration is made and where and how you refer to n.<br><br>I had to use parentheses () instead of square brackets, because Tek-Tips' message parser thinks square brackets are only for text formatting. <p>Octalman<br><a href=mailto: > </a><br><a href= > </a><br>
 
There's two ways of writing the header but first lets wrap your struct in a typedef<br><FONT FACE=monospace><br>typedef struct node {<br>&nbsp;&nbsp;&nbsp;&nbsp;double x,y;<br>} node_t;<br><br>node_t* n;<br><i>/* it just saves having to type &quot;struct node&quot; all the time*/</i><br></font><br>now you can write the header like this<br><FONT FACE=monospace>void readStruct(double d1, double d2);</font><br>or<br><FONT FACE=monospace>void readStruct(node_t n);</font><br>or even<br><FONT FACE=monospace> void readStruct(struct node n);</font><br>note: the last two are eqivalent, you just have to type less.<br>You can now call your function like this<br><FONT FACE=monospace>readStruct(n-&gt;x, n-&gt;y);</font><br>or<br><FONT FACE=monospace>readStruct(n);</font><br>depending on which header you used <p>Chris Packham<br><a href=mailto:kriz@i4free.co.nz>kriz@i4free.co.nz</a><br><a href= > </a><br>A thousand mokeys at a thousand machines, it happened, it got called the internet.
 
All the answers seems to be right. But we should always try to convince the author first , then suggest the better way.<br>Mr Sirbu has declared his structure in a right way.&nbsp;&nbsp;<br>struct node {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double x,y;<br>&nbsp;&nbsp;&nbsp;&nbsp;} *n;<br><br>All he need is to assign memory to &quot;n&quot;. His question is how to pass this n to other function. The answer could be:<br>1) If you want your readstruct function to be a GENERIC one then just define your function like:<br>&nbsp;&nbsp;void ReadStruct(void* n) ;<br>else<br>2) you can define the function like :<br>&nbsp;void ReadStruct(struct node* n) ;<br><br>And call this function like:<br>ReadStruct(n);<br>&nbsp;from your main function. Then in the function ReadStruct you need to allocate memory to n like <br>n = (struct node*) malloc (sizeof(struct node));<br><br>Does this make sense ?<br>Thanx<br>Siddhartha Singh<br><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>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top