使用struct / union的变长记录 - 帮助 [英] variable length record using struct/union - help
问题描述
我想在C中访问可变长度记录,格式为
如下:
+ --- + --- + - ---------- +
| A | L | DATA |
+ --- + --- + ----------- +
A - 一些数据(1 BYTE)>
L - 跟随数据的长度(1 BYTE)
然后实际数据
我想使用struct /访问并表示此记录联合在c,
我该怎么做...
数据是可变长度,L告诉数据的长度(以字节为单位)
跟随...
这样我就可以这种方式访问它:
pData-> A = 56;
pData- > data [pData-> L - 1] =''\ 0'';
现在我要代表
+ --- + --- + ----------- + --- +
| A | L | D A T A | B |
+ --- + --- + ----------- + --- +
其中B - 是一些数据(1字节)
我想这样访问它
pData-> B
(记住它是在可变长度之后数据)
我该怎么办...... ????
-Neo
vk*******@rediffmail.com ( Panchal V)写在
新闻:13 ************************** @ posting.google.c om:
现在我要代表
+ --- + --- + ----------- + --- +
| A | L | D A T A | B |
+ --- + --- + ----------- + --- +
其中B - 是一些数据(1字节)
我想这样访问它
pData-> B
(记住它是在可变长度数据之后)
我该怎么做....? ???
你不能。
-
- 马克 - >
-
Panchal V写道:
我想访问一个可变长度记录在C中,格式如下:
+ --- + --- + ----------- +
| A | L | DATA |
+ --- + --- + ----------- +
A - 一些数据(1字节)
L - 长度数据如下(1 BYTE)
然后是实际数据
我想在c中使用struct / union来访问和表示这条记录,
我该怎么做...
数据是可变长度,L告诉
遵循的数据长度(以字节为单位)......
这样我就可以这种方式访问它:
pData-> A = 56;
pData-> data [pData-> L - 1] =''\ 0'';
现在我要代表
+ --- + - - + ----------- + --- +
| A | L | D A T A | B |
+ --- + --- + ----------- + --- +
其中B - 是一些数据(1字节)
我想这样访问它
pData-> B
(记住它是在可变长度数据之后)
我该怎么做....? ????
你真的不能做你想做的事。但是合理的替代方案可能是定义类似的东西:
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)
并像这样使用它:
struct Record * rptr;
...
unsigned char b = GETB(rptr);
...
SETB(rptr,b);
-nrk。
-Neo
-
删除电子邮件的devnull
Panchal V < VK ******* @ rediffmail.com>在消息中写道
news:13 ************************** @ posting.google.c om ...我想在C中访问一个可变长度的记录,格式如下:
+ --- + --- + ---- ------- +
| A | L | DATA |
+ --- + --- + ----------- +
A - 一些数据(1字节)
L - 长度数据如下(1 BYTE)
然后是实际数据
我想在c中使用struct / union来访问和表示这条记录,
我该怎么做...
数据是可变长度,L告诉
遵循的数据长度(以字节为单位)......
这样我就可以这种方式访问它:
pData-> A = 56;
pData-> data [pData-> L - 1] =''\ 0'';
现在我要代表
+ --- + - - + ----------- + --- +
| A | L | D A T A | B |
+ --- + --- + ----------- + --- +
其中B - 是一些数据(1字节)
我想这样访问它
pData-> B
(记住它是在可变长度数据之后)
我该怎么做....? ???
===============================
/ *使用struct hack和做法。 * /
typedef struct header
{
unsigned char a; / * something * /
unsigned char len; / *数据的数量元素* /
unsigned char数据[1]; / *元素数是谎言* /
}标题,* Header_P;
/ *
*获取unsigned char的值B"以下是
*可变长度的数据。数组。
* /
unsigned char getB(Header_P header_p)
{
return *(header_p-> ; len + header_p->数据);
}
/ *
*设置unsigned char的值B"那个
*跟在数据之后。数组。
* /
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调试在IBM z / OS上运行的系统/ C程序!
ISV升级费用是否过高?检查我们的定制产品开发!
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
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 ->
--
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
"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!
这篇关于使用struct / union的变长记录 - 帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!