Sign in to follow this  
MTclip

Pointer Pointer member access?

Recommended Posts

OK, I have this function that takes a pointer to a struct... and that function calls another function that that acts on that same struct... I am having trouble accessing the members in the second function
[SOURCE]
void terrainCoord2ScreenCoordNPC(NPCSprite** NPC1)
{
   // NPC1->X   =  NPC1->terrainX - g_offset;  // doesnot work
   // *NPC1->X  =  *NPC1->terrainX - g_offset;  // doesnot work
   // NPC1->*X  =  NPC1->*terrainX - g_offset;  // doesnot work

}

void renderNPC(NPCSprite* NPC1)
{
    NPC1->terrainX = 10;  // WORKS FINE
    terrainCoord2ScreenCoordNPC(&NPC1);
}

void main()
{
    // create sprite struct 
    NPCSprite firstNPC;
    renderNPC(&firstNPC);
}


[/SOURCE]
What am i not doing ??? thanks

Share this post


Link to post
Share on other sites
FYI, you don't have to pass it as a pointer to a pointer. In fact, don't. This will work the way you want it to:

void terrainCoord2ScreenCoordNPC(NPCSprite* NPC1)
{
NPC1->X = NPC1->terrainX - g_offset;
}

void renderNPC(NPCSprite* NPC1)
{
NPC1->terrainX = 10; // WORKS FINE
terrainCoord2ScreenCoordNPC(NPC1);
}

void main()
{
// create sprite struct
NPCSprite firstNPC;
renderNPC(&firstNPC);
}

Share this post


Link to post
Share on other sites
Agreed; you'll need a double pointer when you want to change the value of the pointer itself. A single pointer will do if you just want to change the value of what the pointer points to.

That being said, multiple levels of indirection are fun, fun, fun!

But yes, you'll have to do:

(*object)->member;

If you want to access the members of a structure that you have a pointer to a pointer to. The reason is that a unary * is below the -> on the precedence chart.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this