I pass a pointer to a function, I can access struct's members from the pointer.
In this function I need to call another function with the same pointer as argument. I use the ampersand but application stopped.
How to call anotherfunction() from myfunction() ?
bool anotherfunction( const ovrVkGraphicsPipelineParms * parms )
{
myStruct info;
info.layout = parms->program->parmLayout.pipelineLayout; // application error
}
bool myfunction( const ovrVkGraphicsPipelineParms * parms )
{
myStruct info;
info.layout = parms->program->parmLayout.pipelineLayout; // Works
anotherfunction( &parms ); // compile, but application stopped
}
void main()
{
myfunction( &pipelineParms );
}
In C, the &
character before a variable does not mean "this thing is a pointer". No, when you put &
before a variable, it means "make me a pointer to this".
So when you called
myfunction(&pipelineParms);
you had a variable pipelineParms
, and you created a pointer to it, and it was that pointer you passed to myfunction
.
Then, in myfunction
, parms
was a pointer (specifically, a pointer to type ovrVkGraphicsPipelineParms
. Hopefully, pipelineParms
in main
was type ovrVkGraphicsPipelineParms
also.)
So in myfunction
, if you want to pass the pointer on to anotherfunction
, just pass it:
anotherfunction(parms);
We can see that anotherfunction
also accepts a pointer to type ovrVkGraphicsPipelineParms
, so this is correct.
When you tried to call anotherfunction(&parms)
, the &
said, "make me a pointer to this". So you got a pointer to a pointer to type ovrVkGraphicsPipelineParms
, or ovrVkGraphicsPipelineParms **
. That might be fine if that's what anotherfunction
expects, but it's not.
It's not that C has regular variables, and pointers to regular variables. You can have pointers to anything. You can have pointers to pointers, and pointers to pointers to pointers, and theoretically you can have 4- and 5- and 6-level pointers, too, all the way up to 12 or more (although in practice anything beyond 3 is extremely rare).
One more thing: You said
anotherfunction( &parms ); // compile, but application stopped
but I'm guessing it did not, in fact, compile properly. I'm guessing your compiler gave you a warning. Mine says:
warning: incompatible pointer types passing 'const ovrVkGraphicsPipelineParms **' to parameter of type 'const ovrVkGraphicsPipelineParms *'; remove &
If your compiler didn't give you a warning on that line, I encourage you to switch to a better compiler, if at all possible. (A modern compiler would also warn you about void main
, which is also wrong, although it's not causing you any problems.)