Jump to content
  • Advertisement
Sign in to follow this  
MTclip

Pointer Pointer member access?

This topic is 4824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!