Thread: **pp as argument, is it safe? Right way to do it?

  1. #1
    Registered User Rennor's Avatar
    Join Date
    Aug 2006
    Location
    Finland
    Posts
    45

    **pp as argument, is it safe? Right way to do it?

    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.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    That's the way to do it dude!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to List Iterator As Function Argument
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2009, 05:30 AM
  2. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 08:09 AM
  3. Bjarne's exception safe sample
    By George2 in forum C++ Programming
    Replies: 13
    Last Post: 12-28-2007, 05:38 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM