[prog] structures containing structures 2

Conor Daly c.daly at met.ie
Wed May 30 11:01:49 UTC 2007


On Wed, May 30, 2007 at 02:15:22AM -0700 or thereabouts, sena emre wrote:
>    
>   Also another point  is that how should I pass a structure to a function? Is this function prototype declaration suitable for this purpose?
>    
>   void pass( struct solution_t solution);

The form you have given here will pass a _copy_ of the struct to the
function.  This is fine unless you want to _modify_ the original struct
within the function.  In that case, you need to use a pointer to the
function in your prototype:

   void pass( struct solution_t *solution);

and call it with:
   pass(&solution);

    
>   one last question is ...:)  Sometimes I get across to code like this...
>    
>   solution_t *solution = NULL; 
>    
>   what this line tells to the compiler....

This line declares a pointer to struct instead of a struct.  It is set =
NULL so that it will not accidentally point to any data until it is
assigned.  This reduces the likelihood of a program crash or unexpected data
damage if the pointer is incorrectly used.  The next thing you might do with
this pointer is assign it the address of an actual struct:

solution_t sol2;

sol2.cargo[i]=17;

solution = sol2;

Conor
-- 
Conor Daly,                   
Met Eireann, Glasnevin Hill,  
Dublin 9, Ireland             
Ph +3531 8064276 Fax +3531 8064247

*********************************************************************************
This e-mail and any files transmitted with it are confidential and intended solely for the addressee. If you have received this email in error please notify the sender.
This e-mail message has also been scanned for the presence of computer viruses.

Ta an riomhphost seo, agus aon chomhad ata nasctha leis, faoi run agus is don te a seoladh chuige amhain e. Ma tharla go bhfuair tu an riomhphost seo tri dhearmad cuir in iul don te a sheol e led' thoil.

Ta an teachtaireacht riomhphoist seo scuabtha le bogearrai frithvireas.
********************************************************************************
NorthBridge Mail Server id e9yrhg43fncvj3974vjenw



More information about the Programming mailing list