This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Using Packed keywrd (need syntax)


hammr@my-deja.com wrote:
> 
> I am new to gcc and g++. I am using g++ currenlty and I am trying to
> define some packed structures. In other compilers(Diab) I could simply
> typedef as follows:
> typedef struct packed {
>   int a;
>   char b;
>   inc c;
> } MyType;
> or some variation thereof. g++ accepts this, but it thinks I'm defining
> the type called 'packed'!
> 
> I searched the net and found this syntax for gcc,
> typdef struct {
>   int a;
>   char b;
>   inc c;
> } MyType __attribute__((packed));
> which my g++ compiler simply rejects.
> 
> What is the proper syntax? Are pragmas required as well? I'd prefer not
> to use any pragmas.
> --
> That was, without a doubt, my opinion.
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.
You have to attach the attribute to the definition of the struct
_itself_.

e.g.

struct _MyType {
	int a;
	char b;
	int c;
} __attribute__((packed));

typedef struct _MyType Mytype;

Quoting from 'info gcc' <hint hint>

Specifying Attributes of Types
==============================

   The keyword `__attribute__' allows you to specify special attributes
of `struct' and `union' types when you define such types.  This keyword
is followed by an attribute specification inside double parentheses.
Three attributes are currently defined for types: `aligned', `packed',
and `transparent_union'.  Other attributes are defined for functions
(*note Function Attributes::.) and for variables (*note Variable
Attributes::.).

   You may also specify any one of these attributes with `__' preceding
and following its keyword.  This allows you to use these attributes in
header files without being concerned about a possible macro of the same
name.  For example, you may use `__aligned__' instead of `aligned'.

   You may specify the `aligned' and `transparent_union' attributes
either in a `typedef' declaration or just past the closing curly brace
of a complete enum, struct or union type *definition* and the `packed'
attribute only past the closing brace of a definition.

   You may also specify attributes between the enum, struct or union
tag and the name of the type rather than after the closing
brace.         

HTH,
--ag
-- 
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)
mailto:agold@bga.com or mailto:agold@cs.utexas.edu
--
"If you come to a fork in the road, take it." L. P. Berra


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]