Pointer Pointer member access?

Started by
3 comments, last by Omaha 18 years, 10 months ago
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
Advertisement
Try one of these:

(*NPC1)->terrainX(**NPC1).terrainX
perfecto!!!


thanks...
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);}
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.

This topic is closed to new replies.

Advertisement