Article View: gnu.gcc.help
Article #42088Function returning struct
From: Bob Nelson
Date: Mon, 22 Jan 1996 00:00
Date: Mon, 22 Jan 1996 00:00
60 lines
1797 bytes
1797 bytes
The code is shown below. Tried it with GCC 2.6.3 on Sun and with
2.7.2 on Linux where it fails (as shown in the comment).
All three of my MS-DOS compilers have no problem with code with
USE_ARRAY defined.
Why does GCC fail if the return value is accessed directly via
member selection (if the struct member is an array in this case)?
/*
If USE_ARRAY is defined, gcc emits:
gcc -DUSE_ARRAY struct-members.c -o struct-members
struct-members.c: In function `main':
struct-members.c:56: invalid use of non-lvalue array
There's no problem if the structure member is a pointer to char.
*/
#include <stdio.h>
#ifdef USE_ARRAY
#define S_TYPE s[81]
#define S_HOW "an array"
#else
#define S_TYPE *s
#define S_HOW "pointer to char"
#endif
typedef struct {
int i;
char S_TYPE;
} struct_t;
struct_t func(void)
{
static struct_t s = { 100, "string as " S_HOW };
return s;
}
int main(void)
{
struct_t s;
s = func();
printf("%d - %s\n", s.i, s.s); /* OK */
printf("%d - %s\n", func().i, func().s); /* Fails */
return 0;
}
--
=============================================================================
Bob Nelson: Dallas, Texas, U.S.A. - bnelson@netcom.com
Linux for fun, M$ for $$$...and the NFL for what really counts!
=============================================================================
Technical Details
Message-ID:
<bnelsonDLKK9y.3zu@netcom.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,377bfa6c70502ec0,start X-Google-Attributes: gid11318c,public From: bnelson@netcom.com (Bob Nelson) Subject: Function returning struct Date: 1996/01/22 Message-ID: <bnelsonDLKK9y.3zu@netcom.com> X-Deja-AN: 136205972 sender: bnelson@netcom8.netcom.com organization: a computer running Linux newsgroups: gnu.gcc.help