how to return structure

Started by
7 comments, last by matrisking 18 years ago
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.
Advertisement
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.
I assume you mean something along the lines of this: -

// a structuretypedef struct tagMyStruct{    int m_nVal1;    double m_dVal2;}MY_STRUCT;// here are some functions that can manipulate the above structureMY_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 ManipulateMyStructurePtrvoid ManipulateMyStructure( MY_STRUCT& rStruct ){    rStruct.m_nVal1 = 2;    rStruct.m_dVal2 = 8.4d;} // end ManipulateMyStructurevoid ManipulateMyStructure( MY_STRUCT* pStruct ){    if ( pStruct != NULL )    {        pStruct->m_nVal1 = 99;        pStruct->m_dVal2 = 18.9d;    } // end if} // end ManipulateMyStructureint 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.
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.
Sounds like a homework assignment to me.... Copied and pasted straight out.
I agree, Im dissappointed to see this community help cheaters.
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.
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.
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?

This topic is closed to new replies.

Advertisement