Path: utzoo!yunexus!geac!syntron!jtsv16!uunet!seismo!sundc!pitstop!sun!decwrl!purdue!bu-cs!mirror!ima!cfisun!lakart!dg From: dg@lakart.UUCP (David Goodenough) Newsgroups: comp.lang.c Subject: Re: Variable-length messages. Message-ID: <306@lakart.UUCP> Date: 24 Oct 88 16:34:48 GMT Article-I.D.: lakart.306 Organization: Lakart Corporation, Newton, MA Lines: 40 Lloyd sez: > We have a difference of opinion at our site as to what is the most > desirable way to handle variable-length messages in C. This is > basically a difference in philosophies. I'm showing you folks on the > net the two opposing approaches in question here, and I would like to > find out which of these each of you prefers and why. > > One group of us here says it's OK to handle this case as follows: > > struct message { > int msgType; /* msg type code */ > int msgLength; /* length of msg body */ > char msgBody[1]; /* variable length msg body */ > }; > > Another group says that since the 'msgBody[1]' field really isn't one > byte long, its use is misleading and would confuse programmers and > debugging software, not to mention the fact that they feel it isn't > "pure". ..... As an alternative to the above, how useable would the following be: struct message { int msgType; /* msg type code */ int msgLength; /* length of msg body */ char *msgBody; /* variable length msg body */ }; Advantages: Is portable, and doesn't fry the mind of dbx and friends. Disadvantages: Requires an extra malloc(3) call every time you want to do anything. -- dg@lakart.UUCP - David Goodenough +---+ | +-+-+ ....... !harvard!xait!lakart!dg +-+-+ | AKA: dg%lakart.uucp@harvard.harvard.edu +---+