www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to properly pass structs to sub-functions?

reply AEon <aeon2001 lycos.de> writes:
I used an - initially global -  struct with many entries to
let me save all sorts of data. The idea was to let me have
to specify only *one* parameter in all the subfunctions I
use, and to let the subfunctions change the data.

Problem: It turns out data changed in the struct, in the
          subfunction, is not remembered in the calling
          function.

Hopefully someone can point out what I am doing wrong:


aepar_global.d          // Home of the struct, and global vars
---
module aepar_global;
char[] gameOpt;         // Global var
struct global           // The struct!
{
     int[2]      Q3Atime;
};
---


aepar.d                 // Main code
---
import aepar_global;    // Import structs + global vars

int main( char[][] args )
{
     global glb;                 // Create only instance of struct
     q3a_readLOGparse( glb );    // Pass struct to function
     ...
}
---


aepar_p_q3a.d
---
module aepar_p_q3a;
import aepar_global;                // Function headers need struct 
structure

void q3a_readLOGparse( global glb ) // Is this the correct way to pass 
the struct?
{
   ...
     q3a_ripLogline( glb, line.dup, mg );    // Passing on the struct again
   ...
}

int q3a_ripLogline( global glb, char[] line, File mg )
{
     ...
     rip_Time_in_Seconds( glb, line );
     printf("(2) Time (%d:%d)\n", glb.Q3Atime[0], glb.Q3Atime[1]);
     ...
}

int rip_Time_in_Seconds( global glb, char[] line )
{
     ...
     glb.Q3Atime[0] = 12;  glb.Q3Atime[1] = 1199;
     printf(" (1) Time (%d:%d)\n", glb.Q3Atime[0], glb.Q3Atime[1]);
     ...
}
---


Info:

- There is only one instance of the "struct global", called
   glb, and this is created in main().

? Is it a good idea to place the struct in aepar_global.d
   and import it into aepar.d?

- Hopefully the glb instance of the "struct global" is
passed right down to q3a_ripLogline(). This function calls
rip_Time_in_Seconds();, where the two int elements of
Q3Atime are set.

- The print "(1) Time..." shows the numbers, but returning
to q3a_ripLogline() "(2) Time..." looses the values.


I seem to have problems getting my initially "everything is
global" code to work with the more "imports have private
scope" approach.

Any help much appreciated. Also do not hesitate to point out
if I am going about all this - fundamentally - in the wrong
way. If so, what would be the "good" D way?


As always, thanks for reading.

AEon
Sep 18 2008
parent reply BCS <ao pathlink.com> writes:
Reply to AEon,

 I used an - initially global -  struct with many entries to let me
 save all sorts of data. The idea was to let me have to specify only
 *one* parameter in all the subfunctions I use, and to let the
 subfunctions change the data.
 
 Problem: It turns out data changed in the struct, in the
 subfunction, is not remembered in the calling
 function.
 Hopefully someone can point out what I am doing wrong:
 

by default, structs are pass by value, use the "ref" attibut or switch to a class void theFn(ref MyStr str){...}
Sep 18 2008
parent AEon <aeon2001 lycos.de> writes:
BCS wrote:
 Reply to AEon,
 
 I used an - initially global -  struct with many entries to let me
 save all sorts of data. The idea was to let me have to specify only
 *one* parameter in all the subfunctions I use, and to let the
 subfunctions change the data.

 Problem: It turns out data changed in the struct, in the
 subfunction, is not remembered in the calling
 function.

by default, structs are pass by value, use the "ref" attibut or switch to a class void theFn(ref MyStr str){...}

I had been thinking, "if this was C, I must not be passing the struct via pointer"... thank you so much for the very quick answer. Works like a charm now. AEon
Sep 18 2008