• Advertisement
Sign in to follow this  

how to return structure

This topic is 4343 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

Please show me, in a small (but full) C++ program how to make a function that returns a structure. For example, modify the modf(...) function so that instead of returning the integer part and assigning the fractionary part to the 2nd parameter, it will fill a structure with these 2 values. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

struct SFoo
{
int m_iFoo;
float m_fFoo;
};

SFoo Foo(int iFoo, float fFoo)
{
SFoo pFoo = {iFoo, fFoo};

return pFoo;
}

int main(...)
{
SFoo pFoo = Foo(1, 2);

std::cout << pFoo.m_iFoo << std::endl << pFoo.m_fFoo;
}


You should really be using std::pair for that though.

Share this post


Link to post
Share on other sites
I assume you mean something along the lines of this: -



// a structure
typedef struct tagMyStruct
{
int m_nVal1;
double m_dVal2;
}MY_STRUCT;

// here are some functions that can manipulate the above structure
MY_STRUCT& ManipulateMyStructure( void );
MY_STRUCT* ManipulateMyStructurePtr( void );
void ManipulateMyStructure( MY_STRUCT& rStruct );
void ManipulateMyStructure( MY_STRUCT* pStruct );

// this method is not recommended as it will return a reference to a local variable!
MY_STRUCT& ManipulateMyStructure( void )
{
MY_STRUCT foo;
foo.m_nVal1 = 4;
foo.m_dVal2 = 0.6d;
return foo;
} // end ManipulateMyStructure

// make sure the returned variable gets deleted! Again, this method is not recommended.
MY_STRUCT* ManipulateMyStructurePtr( void )
{
MY_STRUCT *pFoo = new MY_STRUCT;
pFoo->m_nVal1 = 9;
pFoo->m_dVal2 = 5.8d;
return pFoo;
} // end ManipulateMyStructurePtr

void ManipulateMyStructure( MY_STRUCT& rStruct )
{
rStruct.m_nVal1 = 2;
rStruct.m_dVal2 = 8.4d;
} // end ManipulateMyStructure

void ManipulateMyStructure( MY_STRUCT* pStruct )
{
if ( pStruct != NULL )
{
pStruct->m_nVal1 = 99;
pStruct->m_dVal2 = 18.9d;
} // end if
} // end ManipulateMyStructure

int main( void )
{
MY_STRUCT foo;
// get the reference
foo = ManipulateMyStructure( );

// get a pointer
MY_STRUCT *pFoo = NULL;
pFoo = ManipulateMyStructure( );
delete pFoo;
pFoo = NULL;

ManipulateMyStructure( foo );
ManipulateMyStructure( pFoo );
} // end main



Those are the main ways to manipulate structures in functions. The exact same principle applies to classes as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by rpg_code_master

// this method is not recommended as it will return a reference to a local variable!
MY_STRUCT& ManipulateMyStructure( void )
{
MY_STRUCT foo;
foo.m_nVal1 = 4;
foo.m_dVal2 = 0.6d;
return foo;
} // end ManipulateMyStructure



Not just not recommended. It WILL cause problems, most likely in the form of an application crash.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I agree, Im dissappointed to see this community help cheaters.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave Hunt
Quote:
Original post by rpg_code_master

// this method is not recommended as it will return a reference to a local variable!
MY_STRUCT& ManipulateMyStructure( void )
{
MY_STRUCT foo;
foo.m_nVal1 = 4;
foo.m_dVal2 = 0.6d;
return foo;
} // end ManipulateMyStructure



Not just not recommended. It WILL cause problems, most likely in the form of an application crash.


smack that guy halfway across the forum board for doing that ;)

warning C4172: returning address of local variable or temporary

And turn treat warnings as errors on :)



And who cares if he cheats? Once a cheater hits the industry the one who worked hard will make more money and the cheater will fail.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
I agree, Im dissappointed to see this community help cheaters.


Perhaps this community didn't immediately recognize that this was a homework assignment.

Share this post


Link to post
Share on other sites
I agree that cheating is bad, shouldn't just give people answers, etc. But 2 things:

1) Maybe he wasn't cheating, why not give him the benefit of the doubt?

2) If you give him the sample code and EXPLAIN it, then he's learning how it works anyways, and isn't that the point of homework and study guides?

People that are just going for grades probably won't make it in the long run anyways, but if the original poster is genuinely eager to know for a project he is working on, for homework, etc, then the purpose of the question doesn't make a difference as long as he's learning something useful.... right?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement