First, I am rather new to pointers, so I am merely verifying that I am going in right direction with what I am coding atm.
Here's the concerning function:
Code:
int EstablishMyBar( BarType **ppBar, int iBarTypeToGet ) {
int status = FOO_FAILURE;
int ret_val;
// Get a pointer to the beginning of the Bar
*ppBar = (BarType *)BarFindType( iBarTypeToGet );
if( *ppBar != NULL )
{
// Check that Bar is valid
ret_val = BarCheck( *ppBar );
if( ( ret_val == BAR_OK ) &&
( (*ppBar)->BarSize == sizeof(BarType) ) )
{
// Success! Valid Bar found!
status = FOO_OK;
}
else
{
// Bar is not valid
status = FOO_INVALID_BAR;
}
}
else
{
// Finding Bar failed
status = FOO_BAR_NOT_FOUND;
}
return status;
}
This function will establish my bar. I wrote this function since I have multiple places in code where I call for BarFindType(...) and then am required to check whether or not it was succesfull with function BarCheck(...) and verifying that size matches with my structure (because, for various good reasons BarFindType returns void pointer).
I found out that passing a NULL pointer to function and then assign address to it within the function does not cause the pointer address change in calling function. So, I had to pass this function a pointer to pointer. And this is what I am unsure about. Yeah, it works (wonders) but. (BUT!) I have had my BEEP! scared out of my BLIP! about using pointers so this is kinda delicate situation for my coding "future" so Guru's! Help me out! Either tell me more better/safe alternatives or let me know "That's the way to do it, dude!"
The BIG question then is; Is this good way of coding?
I call EstablishMyBar from main program like this:
Code:
BarType *pMyBar; // Pointer to my bar
ret_val = EstablishMyBar( &pMyBar, MY_BAR_TYPE1 ); // Establish the bar
(Note that all variable and function names are "foobared" for my secure convenience)
(If it's relevant, all Bars are already in memory, BarFindType() is merely finding one asked and returns void pointer)
Thanks in Advance.