Structures

Hi everyone,

im a new user to the forum, so please bear with me if the way i'm doing this is wrong. So here it goes:

I've started my master thesis and now i have to start programming. Never really gotten in touch with that so i try to get used to everything. Here's a simplified version on what my problem is:

Here we've got the structure:
1
2
3
4
typedef struct something {
Cube_Ptr Cube1;
Cube_Ptr Cube2;
}


Here we've got the code:
1
2
3
4
5
6
7
8
#include <structure.h>

extern int opt_read_cube(TC_Ptr tcOne,Cube_Ptr Cube, char* acFileName)
{
...
var = tcOne->Cube->iValue;
...
}


Okay. To explain, because i dont want to post all the code - tcOne is the main struct which incompases all the Cubes for example and iValue is some datatype (?) which is linked to the Cube_Struct (also not posted). Now here's my problem. The function opt_read_cube is also giving a Cube_Ptr - so when i call the function like this

 
opt_read_cube(tcOne,tcOne->Cube1,"Cube1");


i just want that to work, but all it tells me that 'Cube' is not part of the structure. I totally understand, but how do i get to tell the opt_read_cube to do what it should do?

Thanks in advance!
toxicate20 wrote:
but all it tells me that 'Cube' is not part of the structure
This is because "Cube" is not a part of the object tcOne (from what I can see). It is a parameter provided to your function by the calling operation.

So line 6 should be: var = Cube->iValue;, you can directly invoke it because you have provided it as a part of the functions parameters:

extern int opt_read_cube(TC_Ptr tcOne, Cube_Ptr Cube , char* acFileName)

I hope this helps!
Last edited on
That did the trick, thanks Ink. But what i am wondering about is, how does th tcOne know that it in this case that it can refer to whatever cube i am taking in the opt_read_cube because i always see lines in the code fragments other ppl wrote that things like
 
int ivar = tcOne->Cube1->iValue;

properly work.

As i am providing the function parameter Cube_Ptr when i call the function like
 
opt_read_cube(tcOne,tcOne->Cube1,"Cube1");


in the main() i'm only saying there is a pointer thats called Cube1 and it refers to the structure tcOne. Damn its so hard to explain what i dont understand :(.
toxicate20 wrote:
Damn its so hard to explain what i dont understand :(
Don't worry I think I get the gist of what you mean. :D

toxicate20 wrote:
how does th tcOne know that it in this case that it can refer to whatever cube i am taking in the opt_read_cube
Within the scope of your function:

extern int opt_read_cube(TC_Ptr tcOne, Cube_Ptr Cube , char* acFileName)

you have provided the variable Cube_Ptr Cube to be used within the function. (see the second parameter in your function signature), this is why you can access it direct, in exactly the same way you can access TC_Ptr tcOne and char* acFileName, because they are within the scope of the function.

toxicate20 wrote:
in the main() i'm only saying there is a pointer thats called Cube1 and it refers to the structure tcOne
main() has no bearing (from what I can see) inside this function definition. The only things that matter are the variables declared inside the function and the variables passed as parameters of the function.

In short, tcOne does not "know" anything. The scope of the function defines what can and can't be accessed.

Here is a link that might help clarify: http://msdn.microsoft.com/en-us/library/b7kfh662%28VS.80%29.aspx

I apologies in advance if I've not explained it well, but if your still confused please ask and I'll try to be a little more explicit.

I hope this helps!

P.S: The name is Lnk2019, and don't worry your not the first! :)
Last edited on
Topic archived. No new replies allowed.