473,594 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning a struct from a function - strange behavior

Hello everyone,

Please take a look at the following code:

#include <stdio.h>

typedef struct person {
char name[40];
int age;
} Person;

static Person make_person(voi d);

int main(void) {
printf("%s\n", make_person().n ame);

return 0;
}

static Person make_person(voi d) {
static Person p = { "alexander" , 18 };

return p;
}

The above small program when compiled without the -std=c99 option
(using gcc 4.2.3) gives me a warning:
"warning: format ‘%s’ expects type ‘char *’, but argument 2 has type
‘char[40]’"
and also fails with a segmentation fault when executed.

If I replace the line printf("%s\n", make_person().n ame); with
printf("%s\n", &make_person(). name[0]); everything works as expected.

Why does this happen? Isn't make_person().n ame a pointer to the
array's first element?

Someone replied to this (in the gcc bugzilla), I am quoting the
answer:

"make_person(). name is a non-lvalue array, so it only decays to a
pointer
for C99, not for C90. If you use -std=c99/-std=gnu99 then the
program
works.

The program does not, however, have defined behavior for C99, only
for
C1x. In C99 the lifetime of the array ends at the next sequence
point,
before the call to printf. In C1x it instead ends at the end of the
evaluation of the containing full expression, which is the call to
printf.

I do not believe any changes to GCC are needed to implement this
particular C1x requirement, since GCC discards information about
variables
lifetimes smaller than a function for gimplification and tree
optimizations that may change those lifetimes, so it will in practice
treat the lifetime as being anywhere it cannot show the temporary not
to
be live."

I can't understand why make_person().n ame is not an lvalue array and
only decays to a pointer for C99. Can someone please explain this?

Also what does this guy mean with the line "In C99 the lifetime of the
array ends at the next sequence point,
before the call to printf"? A function call is a sequence point?

I am having a hard time understanding this one, any help appreciated
Thanks for your time

PS. I tried the lcc compiler which compiled the code without warnings/
errors
Oct 7 '08 #1
160 5807
One more question:

Is there any (Person) variable created in main to hold the
make_person() return value?
If this is the case, does all the values from the returned struct be
copied to the variable mentioned above?
Oct 7 '08 #2
DiAvOl <dia...@freemai l.grwrote:
Hello everyone,

Please take a look at the following code:

#include <stdio.h>

typedef struct person {
* char name[40];
* int age;

} Person;

static Person make_person(voi d);

int main(void) {
* printf("%s\n", make_person().n ame);
* return 0;
}

static Person make_person(voi d) {
* static Person p = { "alexander" , 18 };
* return p;
}

The above small program when compiled without the
-std=c99 option (using gcc 4.2.3) gives me a warning:
"warning: format ‘%s’ expects type ‘char *’, but
argument 2 has type ‘char[40]’"
and also fails with a segmentation fault when executed.
That's a bug in gcc 4.2.3 then. [The same segfault
happens with -ansi.]
If I replace the line printf("%s\n", make_person().n ame);
with printf("%s\n", &make_person(). name[0]); everything
works as expected.

Why does this happen?
Because there's a bug in gcc 4.2.3.
Isn't make_person().n ame a pointer to the array's first
element?
Since it's not the operand to an unary & or sizeof operator,
yes.
Someone replied to this (in the gcc bugzilla), I am
quoting the answer:

"make_person(). name is a non-lvalue array, so it only
decays to a pointer for C99, not for C90.
Ask them for chapter and verse. And ask them why
printf("%s\n", "hello") isn't ill formed for the same
reason.

C89 (draft):

"An lvalue is an expression (with an object
type or an incomplete type other than void) that
designates an object."

"Except when it is the operand of the sizeof
operator or the unary & operator, or is a character string
literal used to initialize an array of character type, or
is a wide string literal used to initialize an array with
element type compatible with wchar_t, an lvalue that has
type ``array of type '' is converted to an expression that
has type ``pointer to type '' that points to the initial
member of the array object and is not an lvalue.

C99:

6.3.2.1p1 "An lvalue is an expression (with an object
type or an incomplete type other than void) that designates
an object."

6.3.2.1p3 "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."

An lvalue is only explicitly required in C90. But in either
case, make_person().n ame is an lvalue.

--
Peter
Oct 7 '08 #3
On Mon, 6 Oct 2008 18:34:34 -0700 (PDT), DiAvOl <di****@freemai l.gr>
wrote:
>One more question:

Is there any (Person) variable created in main to hold the
make_person( ) return value?
Officially, it is an implementation detail about which the standard
imposes no requirement.

Practically, if you had ignored the return value, the answer might be
no. Since you use the return value, the answer is almost definitely
yes. But the variable in question is one of those temporary
constructs known only to the compiler. There is no way for you to
access it. Since it has no name, one could argue that it is not a
variable or object.
>If this is the case, does all the values from the returned struct be
copied to the variable mentioned above?
Possibly. Maybe even probably. Under the "how would you tell the
difference" concept, the compiler could probably determine you only
need the member name and not copy the member age.

By the way, since the structure is returned by value, there is no need
for it to be static in your function.

--
Remove del for email
Oct 7 '08 #4
On Oct 7, 9:23*am, DiAvOl <dia...@freemai l.grwrote:
int main(void) {
* printf("%s\n", make_person().n ame);

* return 0;

}
why don't you code it in this way.

int main(void){
Person p;

p = make_person();
printf("%s\n", p.name);

/*printf("%s\n", make_person().n ame);*/
return 0;
}
Oct 7 '08 #5
Peter Nilsson <ai***@acay.com .auwrites:
DiAvOl <dia...@freemai l.grwrote:
>Hello everyone,

Please take a look at the following code:

#include <stdio.h>

typedef struct person {
Â* char name[40];
Â* int age;

} Person;

static Person make_person(voi d);

int main(void) {
Â* printf("%s\n", make_person().n ame);
Â* return 0;
}

static Person make_person(voi d) {
Â* static Person p = { "alexander" , 18 };
Â* return p;
}

The above small program when compiled without the
-std=c99 option (using gcc 4.2.3) gives me a warning:
"warning: format ‘%s’ expects type ‘char *’, but
argument 2 has type ‘char[40]’"
and also fails with a segmentation fault when executed.

That's a bug in gcc 4.2.3 then. [The same segfault
happens with -ansi.]
No, I don't think it's a bug.

[...]
>Someone replied to this (in the gcc bugzilla), I am
quoting the answer:

"make_person() .name is a non-lvalue array, so it only
decays to a pointer for C99, not for C90.

Ask them for chapter and verse. And ask them why
printf("%s\n", "hello") isn't ill formed for the same
reason.
Because "hello" refers to an object of type char[6]; see the
definition of a string literal. There's no object of type Person.

A function returns a value. There isn't necessarily an object
associated with that value. Given:

int func(void) { return 42; }

func returns the value 42; there's no object whose address you could
take, such that dereferencing that address would give you the value
42.

[...]
An lvalue is only explicitly required in C90. But in either
case, make_person().n ame is an lvalue.
I don't believe it is. make_person() yields a value of type Person.
make_person().a ge yields a value of type int. make_person().n ame
reveals an anomaly in the C type system; it should be a value of type
char[40], which should decay to a pointer to the first element of the
corresponding array object, but there is no array object, just a
value.

And this specific case is why the following was added in n1336, the
first C201X draft (6.2.4p7):

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_ 29). 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.

Footnote 29 says:

The address of such an object is taken implicitly when an array
member is accessed.

This new rule means that there is an object of type Person, causing
make_person.nam e() to be an lvalue. In C90 or C99, it's not an
lvalue.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 7 '08 #6
"lo************ ***@gmail.com" <lo************ ***@gmail.comwr ites:
On Oct 7, 9:23*am, DiAvOl <dia...@freemai l.grwrote:
>int main(void) {
* printf("%s\n", make_person().n ame);

* return 0;

}

why don't you code it in this way.

int main(void){
Person p;

p = make_person();
printf("%s\n", p.name);

/*printf("%s\n", make_person().n ame);*/
return 0;
}
Because that doesn't illustrate the point.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 7 '08 #7
DiAvOl wrote:
Hello everyone,

Please take a look at the following code:

#include <stdio.h>

typedef struct person {
char name[40];
int age;
} Person;

static Person make_person(voi d);

int main(void) {
printf("%s\n", make_person().n ame);

return 0;
}

static Person make_person(voi d) {
static Person p = { "alexander" , 18 };

return p;
}

The above small program when compiled without the -std=c99 option
(using gcc 4.2.3) gives me a warning:
"warning: format ‘%s’ expects type ‘char *’, but argument 2 has type
‘char[40]’"
and also fails with a segmentation fault when executed.

If I replace the line printf("%s\n", make_person().n ame); with
printf("%s\n", &make_person(). name[0]); everything works as expected.

Why does this happen? Isn't make_person().n ame a pointer to the
array's first element?

Someone replied to this (in the gcc bugzilla), I am quoting the
answer:

"make_person(). name is a non-lvalue array, so it only decays to a
pointer
for C99, not for C90. If you use -std=c99/-std=gnu99 then the
program
works.

The program does not, however, have defined behavior for C99, only
for
C1x. In C99 the lifetime of the array ends at the next sequence
point,
before the call to printf. In C1x it instead ends at the end of the
evaluation of the containing full expression, which is the call to
printf.

I do not believe any changes to GCC are needed to implement this
particular C1x requirement, since GCC discards information about
variables
lifetimes smaller than a function for gimplification and tree
optimizations that may change those lifetimes, so it will in practice
treat the lifetime as being anywhere it cannot show the temporary not
to
be live."

I can't understand why make_person().n ame is not an lvalue array and
only decays to a pointer for C99. Can someone please explain this?

Also what does this guy mean with the line "In C99 the lifetime of the
array ends at the next sequence point,
before the call to printf"? A function call is a sequence point?

I am having a hard time understanding this one, any help appreciated
Thanks for your time

PS. I tried the lcc compiler which compiled the code without warnings/
errors

Yes, lcc-win compiles and executes correctly your code. As does
MSVC, that correctly executes it.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Oct 7 '08 #8
On Oct 7, 2:57*pm, Keith Thompson <ks...@mib.orgw rote:
"lovecreatesbea ...@gmail.com" <lovecreatesbea ...@gmail.comwr ites:
On Oct 7, 9:23*am, DiAvOl <dia...@freemai l.grwrote:
int main(void) {
* printf("%s\n", make_person().n ame);
* return 0;
}
why don't you code it in this way.
int main(void){
* * * * Person p;
* * * * p = make_person();
* * * * printf("%s\n", p.name);
* * * * /*printf("%s\n", make_person().n ame);*/
* * * * return 0;
}

Because that doesn't illustrate the point.
The op's code is wrong and my revision is right, isn't it?
Oct 7 '08 #9
On 7 Oct, 09:28, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
On Oct 7, 2:57*pm, Keith Thompson <ks...@mib.orgw rote:


"lovecreatesbea ...@gmail.com" <lovecreatesbea ...@gmail.comwr ites:
On Oct 7, 9:23*am, DiAvOl <dia...@freemai l.grwrote:
>int main(void) {
>* printf("%s\n", make_person().n ame);
>* return 0;
>}
why don't you code it in this way.
int main(void){
* * * * Person p;
* * * * p = make_person();
* * * * printf("%s\n", p.name);
* * * * /*printf("%s\n", make_person().n ame);*/
* * * * return 0;
}
Because that doesn't illustrate the point.

The op's code is wrong and my revision is right, isn't it
the point is that it is not obvious why the OPs code is
wrong. It surprised me.

--
Nick Keighley

Oct 7 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3233
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
0
7946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7876
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3859
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3897
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2385
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.