473,765 Members | 1,869 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 #1
18 9400
vk*******@redif fmail.com (Panchal V) wrote in
news:13******** *************** ***@posting.goo gle.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....????


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*******@redi ffmail.com> wrote in message
news:13******** *************** ***@posting.goo gle.com...
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*******@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';

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*******@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.
--
#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 #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(n p)[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.co m> wrote in
comp.lang.c:
"Panchal V" <vk*******@redi ffmail.com> wrote in message
news:13******** *************** ***@posting.goo gle.com...
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.l earn.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.go ogle.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(n p)[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*******@spam cop.net> wrote in message news:<eu******* *************** **********@4ax. com>...
On Mon, 02 Feb 2004 14:37:14 GMT, "xarax" <xa***@email.co m> wrote in
comp.lang.c:
"Panchal V" <vk*******@redi ffmail.com> wrote in message
news:13******** *************** ***@posting.goo gle.com...
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

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
6693
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
16135
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
14904
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
6088
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
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9393
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
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7371
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5272
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
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3921
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
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2800
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.