Passing a reference to a value returned by a function

Mihnea Balta dark_lkml@mymail.ro
Tue Mar 11 16:16:00 GMT 2003


Yes, that's what I meant with "adding a temp var by hand to all the places 
where this stuff occurs" :). I'd like to have as little modification as 
possible to the original code, since that kind of construct occurs very 
often.

I can't make the functions take structs instead of references either, because 
it's a critical section of the code, and the strctures are pretty big.

On Tuesday 11 March 2003 18:05, Ajay Bansal wrote:
> One way id to create a variable & store the return value of
> function_returning_struct in this variable as
>
>  type_t a = function_returning_struct();
>     printf("%d\n", function_taking_reference(a));
>     return 1;
>
> -----Original Message-----
> From: Mihnea Balta [mailto:dark_lkml@mymail.ro]
> Sent: Tuesday, March 11, 2003 9:27 PM
> To: gcc-help@gcc.gnu.org
> Subject: Passing a reference to a value returned by a function
>
>
> Hi.
>
> I have this piece of code (excuse the long function names, it's example
> code
>
> :) ):
>
> #include <stdio.h>
>
> struct type_t{
>     int a, b, c;
> };
>
> type_t function_returning_struct(){
>     type_t  a;
>
>     a.a = 1;
>     return a;
> }
>
> int function_taking_reference(type_t& a){
>     if(a.a == 1)
>         return 1;
>     else
>         return 0;
> }
>
> int main(){
>     printf("%d\n",
> function_taking_reference(function_returning_struct()));
>     return 1;
> }
>
> I'm trying to port code written like this from visualc to gcc. I suppose
> that
> visualc compiles the above piece of code because it creates a temporary
> type_t that holds the return of function_returning_struct(), and passes
> a
> reference to that to function_taking_reference(). However, gcc 3.2.2
> won't
> compile that kind of code.
>
> Is there any way to make gcc work with such code? I'm asking because
> it's in a
> lot of places, and adding a temp var by hand to all the places where
> this
> stuff occurs would be a pain.
>
> Thanks.
>
>
> ------------------------------------------------------------------------
> http://felicitari.mymail.ro/



More information about the Gcc-help mailing list