Passing a reference to a value returned by a function

Ajay Bansal Ajay_Bansal@infosys.com
Tue Mar 11 16:07:00 GMT 2003


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.



More information about the Gcc-help mailing list