delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/01/26/23:06:50

Xref: news2.mv.net comp.os.msdos.djgpp:600
Newsgroups: comp.os.msdos.djgpp
From: bnelson AT netcom DOT com (Bob Nelson)
Subject: Function returning struct
Message-ID: <bnelsonDLKKGw.4Bq@netcom.com>
Organization: a computer running Linux
Date: Mon, 22 Jan 1996 06:24:32 GMT
Lines: 57
Sender: bnelson AT netcom8 DOT netcom DOT com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

The code below is self-explanatory. Any ideas why GCC/DJGPP fails to
compile this with USE_ARRAY defined. FWIW...Borland C 3.1, Turbo
C 2.0 and Microsoft C 6.0a have no problem -- with all warnings
enabled in the case of the latter 3 compilers.

/*
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 AT netcom DOT com
      Linux for fun, M$ for $$$...and the NFL for what really counts!
=============================================================================

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019