473,399 Members | 3,106 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,399 software developers and data experts.

Gap of two 0x00 between structs.....

Don
Hi NG. I have the code below my question.
I would like to copy the content of measuredata to the MyBuffer from place
nr 20 (MyBuffer[19]). The problem with this code is that between the two
structs I get two empty bytes :-(. Like measuredata.struct1.measure13 are
placed at MyBuffer[19+786] and when I print this it displays oxBF fine, but
when I print
MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
two 0x00 and 0x00.....Can I workaround this or is this something to do with
the struct type???

Best Regards
Don

unsigned char MyBuffer[2000];

struct mystruct1{
unsigned char measure1[256];
unsigned short measure2[256];
unsigned char measure3;
unsigned short measure4;
unsigned short measure5;
unsigned short measure6;
unsigned short measure7;
unsigned short measure8;
unsigned short measure9;
unsigned char measure10;
unsigned char measure11;
unsigned char measure12;
unsigned char measure13;
};

struct mystruct2{
unsigned int measure14;
unsigned int measure15;
unsigned short measure16;
unsigned short measure17;
unsigned short measure18;
unsigned short measure19;
unsigned short measure20;
unsigned short measure21;

unsigned int measure22;
unsigned int measure23;
unsigned int measure24;
unsigned int measure25;
unsigned int measure26;
unsigned int measure27;

unsigned int measure28;
unsigned int measure29;
unsigned int measure30;
unsigned int measure31;
};
struct measurement_data{
struct mystruct1 struct1;
struct mystruct2 struct2;
}measuredata;

int main(){
int i= 0;

for(i=0;i<200;i++)
measuredata.struct1.measure1[i] = 0x55;

for(i=0;i<100;i++)
measuredata.struct1.measure2[i] = 0xFF11;

measuredata.struct1.measure9 = 0x1133;

measuredata.struct2.measure16 = 0x12345678;
measuredata.struct2.measure23 = 0xFFFFFFFF;
measuredata.struct2.measure26 = 0x08080808;
measuredata.struct2.measure30 = 0x00000001;
measuredata.struct2.measure31 = 0xDEADBEEF;

//The transition between struct1 and struct2
measuredata.struct1.measure13 = 0xBF;
measuredata.struct2.measure14 = 0x12345678;

//Copy measuredata struct with variable values to the MyBuffer
memcpy(&MyBuffer[19], (unsigned char *) &measuredata, sizeof(struct
measurement_data));

return 0;

}
Nov 13 '05 #1
3 1732
Don wrote:
Hi NG. I have the code below my question.
I would like to copy the content of measuredata to the MyBuffer from place
nr 20 (MyBuffer[19]). The problem with this code is that between the two
structs I get two empty bytes :-(. Like measuredata.struct1.measure13 are
placed at MyBuffer[19+786] and when I print this it displays oxBF fine, but
when I print
MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
two 0x00 and 0x00.....Can I workaround this or is this something to do with
the struct type???

Best Regards
Don

unsigned char MyBuffer[2000];


The compiler is allowed to add padding bytes between fields in a
structure and also at the end of the structure.

Some compilers offer a "packed" option which forces the compiler
remove the padding (I've had problems with this option on
different compilers). This option is NOT standard.

Another option is to use a pointer to an unsigned character
then cast to whatever type. In this manner, you can access
memory at the finest detail without messing with any padding
that _may_ be imposed by the compiler.

Just remember that the size of a structure may not be equal
to the size of its fields.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #2
On Wed, 1 Oct 2003 15:43:25 +0200, "Don" <no**@spam.dk> wrote in
comp.lang.c:
Hi NG. I have the code below my question.
I would like to copy the content of measuredata to the MyBuffer from place
nr 20 (MyBuffer[19]). The problem with this code is that between the two
structs I get two empty bytes :-(. Like measuredata.struct1.measure13 are
placed at MyBuffer[19+786] and when I print this it displays oxBF fine, but
when I print
MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
two 0x00 and 0x00.....Can I workaround this or is this something to do with
the struct type???

Best Regards
Don


[code snipped]

http://www.eskimo.com/~scs/C-faq/q2.13.html

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #3
# MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
# displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
# MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
# two 0x00 and 0x00.....Can I workaround this or is this something to do with
# the struct type???

C structs cannot be portably used to stencil a structure on a byte stream. To be
portable you need to move bytes to and from a struct yourself. Nonportably your
compiler may have pragmas or other means to force byte alignment; consult your
compiler documentation if you want to go this route.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Who's leading this mob?
Nov 13 '05 #4

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

Similar topics

4
by: news.microsoft.com | last post by:
Hi, I am using structs and am also using property accessors to access those private member fields... TO me this is a good way of handling them, but I find alot of people using direct access to...
3
by: Joseph Suprenant | last post by:
Quick question hope someone can help. I am sending a file accross a radio, i am limited to using only chars. I get all the data on the receiving end but everytime it gets a 0x00 it is treated as...
8
by: baumann | last post by:
hi all, i hope printf("%#04x\n", 0); will output 0x00, but visual c++ studio 6 outputs 0000. how can i get 0x00? thanks
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
6
by: jasn | last post by:
Hello I am getting the following error message when I try and send an XML sting to a web service, I read somewhere that most web services prefer ascii and some throw errors when using unicode so...
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.