Quantcast
Viewing all articles
Browse latest Browse all 22157

What is a lifetime of a function return value?

I read about the return values between function calls,
and experimented with the following code snippet :

/* file structaddr.c */
#include <stdio.h>    
#define MSIZE 10

struct simple
{   
    char c_str[MSIZE];
};
struct simple xprint(void)
{
    struct simple ret = { "Morning !" };
    return ret;
}
int main(void)
{   
    printf("Good %s\n", xprint().c_str);    
    return 0;
}

The code is compiled without errors and warnings .
Tested with GCC 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) and Visual C++ compilers .

 gcc -m32 -std=c99 -Wall -o test  structaddr.c  
 cl -W3 -Zi -GS -TC -Fetest structaddr.c

Output :
Good Morning !

I'm a little confused by the result .
The code is written correctly ?

My Question :

  • What is the visibility of the function return value( array from a struct in above example ), and how to properly access them ?

  • Where ends lifetime of a return value ?


Viewing all articles
Browse latest Browse all 22157

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>