473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 #1
18 9343
vk*******@rediffmail.com (Panchal V) wrote in
news:13**************************@posting.google.c om:
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.

--
- Mark ->
--
Nov 14 '05 #2
nrk
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';

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 really do what you want. But a reasonable alternative might be to
define something like:

struct Record {
unsigned char A;
unsigned char L;
unsigned char *data;
};

#define GETB(rptr) ((rptr)->data[(rptr)->L])
#define SETB(rptr, val) ((rptr)->data[(rptr)->L] = val)

and use it like this:
struct Record *rptr;
...
unsigned char b = GETB(rptr);
...
SETB(rptr, b);

-nrk.
-Neo


--
Remove devnull for email
Nov 14 '05 #3
"Panchal V" <vk*******@rediffmail.com> wrote in message
news:13**************************@posting.google.c om...
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....????


===============================
/* Use the "struct hack" approach. */
typedef struct header
{
unsigned char a; /* something */
unsigned char len; /* number of "data" elements */
unsigned char data[1]; /* element count is a lie */
} Header, * Header_P;
/*
* get the value of "unsigned char B" that follows
* the variable-length "data" array.
*/
unsigned char getB(Header_P header_p)
{
return *(header_p->len + header_p->data);
}

/*
* set the value of "unsigned char B" that
* follows the "data" array.
*/
void setB(Header_P header_p, unsigned char b)
{
*(header_p->len + header_p->data) = b;
}
===============================
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!
Nov 14 '05 #4
On 2 Feb 2004 06:00:01 -0800, vk*******@rediffmail.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';

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....????
If, in your structure, you represent your "data" with a _pointer_ and
allocate storage for it dynamically on-the-fly as you are reading your
data in, then you'll be able to use your desired syntax (since
subscripting works for pointers "as if" they were arrays).
-leor

-Neo


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #5
On 2 Feb 2004 06:00:01 -0800, vk*******@rediffmail.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.
--
#include <standard.disclaimer>
_
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 #6
> 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


typedef unsigned char *NEO_PTR;
#define NEO_A(np) ((np)[0])
#define NEO_L(np) ((np)[1])
#define NEO_DATA(np) ((np) + 2)
#define NEO_DATA_END(np) (NEO_DATA(np) + NEO_L(np))
#define NEO_B(np) (NEO_DATA_END(np)[0])

You can't access with the pointer syntax as you described.
Also, remember that these are macros so don't go "++" in them, etc.

NEO_PTR np = somewhere;
printf("B=%d, L=%d, A=%d, data=%s",
NEO_B(np), NEO_L(np), NEO_A(np), NEO_DATA(np));
NEO_B(np) = 15;
Nov 14 '05 #7
On Mon, 02 Feb 2004 14:37:14 GMT, "xarax" <xa***@email.com> wrote in
comp.lang.c:
"Panchal V" <vk*******@rediffmail.com> wrote in message
news:13**************************@posting.google.c om...
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....????


===============================
/* Use the "struct hack" approach. */


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
ol*****@inspire.net.nz (Old Wolf) wrote in message news:<84**************************@posting.google. com>...
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


typedef unsigned char *NEO_PTR;
#define NEO_A(np) ((np)[0])
#define NEO_L(np) ((np)[1])
#define NEO_DATA(np) ((np) + 2)
#define NEO_DATA_END(np) (NEO_DATA(np) + NEO_L(np))
#define NEO_B(np) (NEO_DATA_END(np)[0])

You can't access with the pointer syntax as you described.
Also, remember that these are macros so don't go "++" in them, etc.

NEO_PTR np = somewhere;
printf("B=%d, L=%d, A=%d, data=%s",
NEO_B(np), NEO_L(np), NEO_A(np), NEO_DATA(np));
NEO_B(np) = 15;


This approach seems to be good as compared to other hacks... if
side-effects are taken care of (sequence points) etc.

Thanks for your views...

- Neo
Nov 14 '05 #9
Jack Klein <ja*******@spamcop.net> wrote in message news:<eu********************************@4ax.com>. ..
On Mon, 02 Feb 2004 14:37:14 GMT, "xarax" <xa***@email.com> wrote in
comp.lang.c:
"Panchal V" <vk*******@rediffmail.com> wrote in message
news:13**************************@posting.google.c om...
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....????


===============================
/* Use the "struct hack" approach. */


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


What's undefined?

-Neo
Nov 14 '05 #10
Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> wrote in message news:<ct********************************@4ax.com>. ..
On 2 Feb 2004 06:00:01 -0800, vk*******@rediffmail.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*******@spamcop.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.disclaimer>
_
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*******@rediffmail.com (Panchal V) wrote:

Kevin D. Quitt <KQ**********@IEEIncUNMUNG.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.disclaimer>
_
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**********@IEEIncUNMUNG.com> wrote:
On Tue, 03 Feb 2004 03:56:21 GMT, Jack Klein <ja*******@spamcop.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.disclaimer>
_
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.google.c om:
> 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.google.c om:
>> 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
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
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
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...
8
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. ...
9
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...
2
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...
2
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.