473,770 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable length record using struct/union - help

I want to access a variable length record in C, the format is as
follows :

+---+---+-----------+
| A | L | D A T A |
+---+---+-----------+
A - Some Data (1 BYTE)
L - Length the Data that follows (1 BYTE)
then actual data

I want to access and represent this record using struct/union in c,
how can I do it...
Data is variable length, L tell the length of data (in bytes) that
follows...
So that I can access it in this manner :
pData->A = 56;
pData->data[pData->L - 1] = '\0';

Now I want to represent
+---+---+-----------+---+
| A | L | D A T A | B |
+---+---+-----------+---+

where B - is some data ( 1 byte)
I want to access it like this
pData->B
(Remember it is after variable length data)

How can I do it....????

-Neo
Nov 14 '05
18 9402
Kevin D. Quitt <KQ**********@I EEIncUNMUNG.com > wrote in message news:<ct******* *************** **********@4ax. com>...
On 2 Feb 2004 06:00:01 -0800, vk*******@redif fmail.com (Panchal V) wrote:

I want to access a variable length record in C, the format is as
follows :

+---+---+-----------+
| A | L | D A T A |
+---+---+-----------+
A - Some Data (1 BYTE)
L - Length the Data that follows (1 BYTE)
then actual data

I want to access and represent this record using struct/union in c,
how can I do it...
Data is variable length, L tell the length of data (in bytes) that
follows...
So that I can access it in this manner :
pData->A = 56;
pData->data[pData->L - 1] = '\0';


struct
{
char A;
char L;
char data[];
} fred;

struct fred *pData;

Now I want to represent
+---+---+-----------+---+
| A | L | D A T A | B |
+---+---+-----------+---+

where B - is some data ( 1 byte)
I want to access it like this
pData->B
(Remember it is after variable length data)

How can I do it....????


You can't by name. The struct hack^W^W flexible array member has to be
the last member of the structure.

pData->data[ pData->L ] points to B.


char data[];
This doesn't seem to work on All implementations (compilers)

I'm using c51 by KEIL, it reports unknown array size...

And lets say we define one more variable after it, compiler starts
complaining, in GCC too...

-Neo
Nov 14 '05 #11
On Tue, 03 Feb 2004 03:56:21 GMT, Jack Klein <ja*******@spam cop.net>
wrote:
/* Use the "struct hack" approach. */


No, don't, it's undefined behavior.


No, it isn't. It's part of C99, although they call it "flexible array
member".
--
#include <standard.discl aimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #12
On 3 Feb 2004 00:54:49 -0800, vk*******@redif fmail.com (Panchal V) wrote:

Kevin D. Quitt <KQ**********@I EEIncUNMUNG.com > wrote in message news:<ct******* *************** **********@4ax. com>...
You can't by name. The struct hack^W^W flexible array member has to be
the last member of the structure.
char data[];
This doesn't seem to work on All implementations (compilers)

I'm using c51 by KEIL, it reports unknown array size...


Then it's not a C99 compiler. Per the Standard, 6.7.2.1:

16 As a special case, the last element of a structure with more than one
named member may have an incomplete array type; this is called a flexible
array member. With two exceptions, the flexible array member is ignored.
First, the size of the structure shall be equal to the offset of the last
element of an otherwise identical structure that replaces the flexible
array member with an array of unspecified length.106) Second, when a . (or
->) operator has a left operand that is (a pointer to) a structure with a
flexible array member and the right operand names that member, it behaves
as if that member were replaced with the longest array (with the same
element type) that would not make the structure larger than the object
being accessed; the offset of the array shall remain that of the flexible
array member, even if this would differ from that of the replacement
array. If this array would have no elements, it behaves as if it had one
element but the behavior is undefined if any attempt is made to access
that element or to generate a pointer one past it.
And lets say we define one more variable after it, compiler starts
complaining, in GCC too...


Of course, because the flexible array member must be the last member.
--
#include <standard.discl aimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #13
Kevin D. Quitt <KQ**********@I EEIncUNMUNG.com > wrote:
On Tue, 03 Feb 2004 03:56:21 GMT, Jack Klein <ja*******@spam cop.net>
wrote:
/* Use the "struct hack" approach. */


No, don't, it's undefined behavior.


No, it isn't. It's part of C99, although they call it "flexible array
member".


But what xanax (to whom Jack Klein replied) demonstrated wasn't a C99
flexible array member, but a C89 mini-array-and-malloc struct hack. And
that _is_ undefined.

Richard
Nov 14 '05 #14
On Tue, 03 Feb 2004 16:08:41 GMT, rl*@hoekstra-uitgeverij.nl (Richard Bos)
wrote:
But what xanax (to whom Jack Klein replied) demonstrated wasn't a C99
flexible array member, but a C89 mini-array-and-malloc struct hack. And
that _is_ undefined.


Indeed - my bad.

--
#include <standard.discl aimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #15
> > struct
{
char A;
char L;
char data[];
} fred;
char data[];
This doesn't seem to work on All implementations (compilers)

I'm using c51 by KEIL, it reports unknown array size...


This is a very horrible compiler (probably you have no choice though?)
You may find you have to employ many non-standard hacks, to get
anything useful done.

Go
char data[1];
instead.
Nov 14 '05 #16
ol*****@inspire .net.nz (Old Wolf) wrote in
news:84******** *************** ***@posting.goo gle.com:
> struct
> {
> char A;
> char L;
> char data[];
> } fred;
char data[];
This doesn't seem to work on All implementations (compilers)

I'm using c51 by KEIL, it reports unknown array size...


This is a very horrible compiler (probably you have no choice though?)


It is a *very good* C51 compiler for the 8051. There are other choices but
this is one of the best. As for C you cannot use char data[];
You may find you have to employ many non-standard hacks, to get
anything useful done.

Go
char data[1];
instead.


You mean you must do char data[1] instead since C89 will not allow char
data[]. Keil good, undimensioned arrays - not permitted.

--
- Mark ->
--
Nov 14 '05 #17
> >> I'm using c51 by KEIL, it reports unknown array size...

This is a very horrible compiler (probably you have no choice though?)


It is a *very good* C51 compiler for the 8051. There are other choices but
this is one of the best. As for C you cannot use char data[];


Perhaps I just have an old version then? (V4.01)
Or perhaps I just remember hours and hours of mucking around with
platform-specific issues and linker memory allocation bugs (eg.
putting concurrently-used variables at overlapping locations),
and am tarring the compiler with the same brush.
You may find you have to employ many non-standard hacks, to get
anything useful done.

Go
char data[1];
instead.


You mean you must do char data[1] instead since C89 will not allow char
data[]. Keil good, undimensioned arrays - not permitted.


I didn't mean to imply that Keil was bad for requiring "char data[1]"
(although I do consider accessing members past the end of an
array to be a 'nonstandard hack').
Nov 14 '05 #18
ol*****@inspire .net.nz (Old Wolf) wrote in
news:84******** *************** ***@posting.goo gle.com:
>> I'm using c51 by KEIL, it reports unknown array size...
>
> This is a very horrible compiler (probably you have no choice
> though?)


It is a *very good* C51 compiler for the 8051. There are other choices
but this is one of the best. As for C you cannot use char data[];


Perhaps I just have an old version then? (V4.01)


Old would be an understatment. My god man, upgrade now!

:-)

--
- Mark ->
--
Nov 14 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1753
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure: struct meta_segment { long double id; long double num; long double mean; bool done;
10
6695
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
14
5849
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I want to store data like a database: ---------------------------------- Custumer:
8
3325
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. However, the DEBUG_EVENT structure is defined as a union, and the size and contents vary depending on the event code contained in the header. typedef struct _DEBUG_EVENT { DWORD dwDebugEventCode; DWORD dwProcessId;
9
8217
by: Danny Mavromatis | last post by:
I have a chunk of VC.NET code (below) that I need to convert to VB.NET syntax. Could someone help me get started? I'm new to structures and unions and I don't understand how to nest then in VB.NET. ' ----- VC.NET CODE THAT I NEED TO CONVERT TO VB.NET ---- struct R_OMNI_LINK_MESSAGE { //unsigned char StartChar; unsigned char MessageLength; union {
2
16138
by: cr55 | last post by:
I was wondering if anyone can help me with this programming code as i keep getting errors and am not sure how to fix them. The error code displayed now is error: C2228: left of '.rent' must have class/struct/union type.The problem area is underlined. Any help will be greatly appreciated. #include <c:\cpp\input.h> #include < time.h> #define SIZE 20 struct Cust{ int custno; char fname;
2
14906
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
18
6089
by: Bryan Parkoff | last post by:
I hate using struct / union with dot between two words. How can I use one word instead of two words because I want the source code look reading clear. three variables are shared inside one variable. I manipulate to change 8-bit data before it causes to change 16-bit data and 32-bit data. For example. union {
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.