添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
I am trying to understand someone else's code so as to generate a new version of it but I keep coming across the term 'PACKED' in typedef's. What does this do?
Any ideas would be appreciated... PACKED doesn't appear in the index of Kernighan and Ritchie's book so I guess it is not an ANSI standard.
Cheers
-Penfold your question is a little confusing, if you could post some code maybe then we could see what is going on.
I am just assuming that "PACKED" is a typedefined structure.
Code:
typedef struct  {
       char variable_ch;
       int   variable_int;
       } PACKED;
you may find this declared at the pre-processor stage of the coding, or within a header file. This allows you to use the structure as data type..so
Code:
struct packed variable;
/* can be used like this */
PACKED variable;
/* and members accessed as follows */
variable.variable_ch = getch();
Code:
#define PACKED packed
with a comment that this is required on some compilers but not all.
Then in the code, the instances of this are :
Code:
typedef PACKED struct
	/* elements of structure */
} FooBarStruct;
or...
Code:
typedef PACKED union
	/* elements of the union */
} FooBarUnion;
however, there are some (fewer) structs and unions that are defined in the style I am accustomed, namely :
Code:
typedef union
	/* elements of the union */
} UnpackedUnion;
Hope this clarifies the matter.
Sorry if you really wanted the actual contents of the structs, etc. I am reasonably confident with my C ability, and I can follow almost all of the rest of the code.
The code is very hardware specific, hence the mass of structs and unions which are necessary to simplify data structures externally imposed by the hardware this runs on. If it matters (though I doubt it) the target is a Motorola ColdFire processor and I am attempting to understand it so as to port it to a PowerPC with increased functionality.
Cheers
-Penfold There can be gaps inbetween members of structures, the PACKED signals the compiler so that there's no gaps.
It's not ansi c though just about all compilers have away of doing it.
  • C++ Tutorial
  • 5 ways you can learn to program faster
  • The 5 Most Common Problems New Programmers Face
  • How to set up a compiler
  • 8 Common programming Mistakes
  • What is C++11?
  • Creating a game, from start to finish Recent additions subscribe to a feed
  • How to create a shared library on Linux with GCC - December 30, 2011
  • Enum classes and nullptr in C++11 - November 27, 2011
  • Learn about The Hash Table - November 20, 2011
  • Rvalue References and Move Semantics in C++11 - November 13, 2011
  • C and C++ for Java Programmers - November 5, 2011
  • A Gentle Introduction to C++ IO Streams - October 10, 2011
  •