Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7052491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:23:24+00:00 2026-05-28T03:23:24+00:00

The following simple code segfaults under gcc 4.4.4 #include<stdio.h> typedef struct Foo Foo; struct

  • 0

The following simple code segfaults under gcc 4.4.4

#include<stdio.h>

typedef struct Foo Foo;
struct Foo {
    char f[25];
};

Foo foo(){
    Foo f = {"Hello, World!"};
    return f;
}

int main(){
    printf("%s\n", foo().f);
}

Changing the final line to

 Foo f = foo(); printf("%s\n", f.f);

Works fine. Both versions work when compiled with -std=c99. Am I simply invoking undefined behavior, or has something in the standard changed, which permits the code to work under C99? Why does is crash under C89?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:23:24+00:00Added an answer on May 28, 2026 at 3:23 am

    I believe the behavior is undefined both in C89/C90 and in C99.

    foo().f is an expression of array type, specifically char[25]. C99 6.3.2.1p3 says:

    Except when it is the operand of the sizeof operator or the unary
    & operator, or is a string literal used to initialize an array, an
    expression that has type “array of type” is converted to an
    expression with type “pointer to type” that points to the initial
    element of the array object and is not an lvalue. If the array object
    has register storage class, the behavior is undefined.

    The problem in this particular case (an array that’s an element of a structure returned by a function) is that there is no “array object”. Function results are returned by value, so the result of calling foo() is a value of type struct Foo, and foo().f is a value (not an lvalue) of type char[25].

    This is, as far as I know, the only case in C (up to C99) where you can have a non-lvalue expression of array type. I’d say that the behavior of attempting to access it is undefined by omission, likely because the authors of the standard (understandably IMHO) didn’t think of this case. You’re likely to see different behaviors at different optimization settings.

    The new 2011 C standard patches this corner case by inventing a new storage class. N1570 (the link is to a late pre-C11 draft) says in 6.2.4p8:

    A non-lvalue expression with structure or union type, where the
    structure or union contains a member with array type (including,
    recursively, members of all contained structures and unions) refers to
    an object with automatic storage duration and temporary lifetime.
    Its lifetime begins when the expression is evaluated and its initial
    value is the value of the expression. Its lifetime ends when the
    evaluation of the containing full expression or full declarator ends.
    Any attempt to modify an object with temporary lifetime results in
    undefined behavior.

    So the program’s behavior is well defined in C11. Until you’re able to get a C11-conforming compiler, though, your best bet is probably to store the result of the function in a local object (assuming your goal is working code rather than breaking compilers):

    [...]
    int main(void ) {
        struct Foo temp = foo();
        printf("%s\n", temp.f);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine the following simple code: public void F<T>(IList<T> values) where T : struct {
I’ve got the following code: #include <iostream> using namespace std; int main() { char*
Consider the following simple code pattern: foreach(Item item in itemList) { if(item.Foo) { DoStuff(item);
look at the following simple code: #include <iostream> #include <string> using namespace std; int
Please take a look at the following simple code: class Foo { public: Foo(){}
I use following simple code to include all files from common folder. $path=array(); $ds=DIRECTORY_SEPARATOR;
i have the following simple code, but it doesn,t work <ul> <li id=one onmouseover=this.style.background-color='white';>
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have got the following very simple code: function init() { var articleTabs =
My problem is a bit more complex than using the following simple JavaScript code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.