473,405 Members | 2,338 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,405 software developers and data experts.

Member layout of structures and type convertions

Suppose, I have two customized types here:

typedef struct
{
int dt_qual;
int dec_exp;
int dec_pos;
int dec_ndgts;
/* char dec_dgts[8]; */
} datetime;

typedef struct
{
int dt[4]; /* [6] */
} dtime;

1. Can value of variables of these two types above be transformed each
other correctly always? Suppose I copy bytes from variable typed
datetime to variable typed dtime using memcpy(), can I get the value of
fields in struct datetime through dtime.dt[i]? For example:

datetime *pdate = (datetime *)malloc(sizeof(*pdate));
dtime sdate;
/* initializaion omitted */
memcpy(&sdate, pdate, sizeof(*pdate));

then, are the following conditional expressions true?

sdate.dt[0] == pdate->dt_qual;
sdate.dt[1] == pdate->dec_exp;
sdate.dt[2] == pdate->dec_pos;
sdate.dt[3] == pdate->dec_ndgts;

2. Type convertions are valid for many data types, but why convertions
(even explicitly) between structs (non-scalar types?...) are forbiden?
What's the consideration under the limitation?

Nov 23 '05 #1
3 1248
On 21 Nov 2005 20:31:30 -0800, "lovecreatesbeauty"
<lo***************@gmail.com> wrote:
Suppose, I have two customized types here:

typedef struct
{
int dt_qual;
int dec_exp;
int dec_pos;
int dec_ndgts;
/* char dec_dgts[8]; */
} datetime;

typedef struct
{
int dt[4]; /* [6] */
} dtime;

1. Can value of variables of these two types above be transformed each
other correctly always? Suppose I copy bytes from variable typed
Yes, by copying from member to member.
datetime to variable typed dtime using memcpy(), can I get the value of
fields in struct datetime through dtime.dt[i]? For example:
No, this is not portable. You are guaranteed that dt and dt_qual both
are mapped at the beginning of their respective structures. You are
also guaranteed that dt[1] follows dt[0] immediately. You are not
guaranteed that dec_exp follows dt_qual with no intervening padding.

datetime *pdate = (datetime *)malloc(sizeof(*pdate));
Don't cast the return from malloc. It seldom helps and can hide a
diagnostic you really want to see.
dtime sdate;
/* initializaion omitted */
memcpy(&sdate, pdate, sizeof(*pdate));
There is no guarantee that *pdate and sdate have the same size.

then, are the following conditional expressions true?

sdate.dt[0] == pdate->dt_qual;
sdate.dt[1] == pdate->dec_exp;
sdate.dt[2] == pdate->dec_pos;
sdate.dt[3] == pdate->dec_ndgts;
No.

2. Type convertions are valid for many data types, but why convertions
(even explicitly) between structs (non-scalar types?...) are forbiden?
What's the consideration under the limitation?


See above. Furthermore, each structure declaration creates a new
unique type.
<<Remove the del for email>>
Nov 23 '05 #2
Suppose, I have two customized types here:

typedef struct
{
int dt_qual;
int dec_exp;
int dec_pos;
int dec_ndgts;
/* char dec_dgts[8]; */
} datetime;

typedef struct
{
int dt[4]; /* [6] */
} dtime;

1. Can value of variables of these two types above be transformed each
other correctly always? Suppose I copy bytes from variable typed
datetime to variable typed dtime using memcpy(), can I get the value of
fields in struct datetime through dtime.dt[i]? For example: 2. Type convertions are valid for many data types, but why convertions
(even explicitly) between structs (non-scalar types?...) are forbiden?
What's the consideration under the limitation?


Converting between the two structures is not portable. The compiler
might have inserted extra pad bytes. In your specific example this is
unlikely but such conversions are not recommended.

The following article might help:

http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based Real-time and Embedded System Design Tool

Nov 23 '05 #3
On 21 Nov 2005 20:31:30 -0800, "lovecreatesbeauty"
<lo***************@gmail.com> wrote:
Suppose, I have two customized types here:

typedef struct
{
int dt_qual;
int dec_exp;
int dec_pos;
int dec_ndgts;
/* char dec_dgts[8]; */
} datetime;

typedef struct
{
int dt[4]; /* [6] */
} dtime;

1. Can value of variables of these two types above be transformed each
other correctly always? Suppose I copy bytes <snip>


Elements of an array are required to be contiguous; as others have
noted, members of a struct are not, although there is no good reason
an implementation should add padding between members of the same type
as here, and I know of none that does.

I would add however that your commented-out part, assuming 2 ints
"cover" 8 characters, is also not guaranteed and will in fact fail on
quite a few real implementations.

- David.Thompson1 at worldnet.att.net
Dec 5 '05 #4

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

Similar topics

0
by: Andy Eshtry | last post by:
Dear Friends I have a urgent problem to solve. I have created a class and its collection and then bind it to a datagrid but got the following error. Please help. Thanks in advance. Andy Eshtry ...
2
by: Jurgen Oerlemans | last post by:
Hello, I want to change to forecolor of a label. The label is named "lblDevice1" I create a variable called Var Dim Var Var = "lblDevice1" So the next step is: Var.ForeColor =...
5
by: jmd | last post by:
hello. i am trying VC++ 7.1 (with vsnet 2003). my example : class Complex { public: Complex ( float re_ = 0.0, float im_ = 0.0 ) : re(re_), im(im_) {} float Re () { return ( re ); } float...
1
by: dave | last post by:
.. Line 38: Function FormatURL5(ByVal strArgument) As String Line 39: If strArgument.length > 0 Then Line 40: Return ("<INPUT id='mmmm' type='checkbox'...
0
by: mazda_88 | last post by:
I have a number of tables inside a dataset. I have a repeater that I need to bind to. The data that is needed resides in two tables in the dataset. Here is the code that I'm using: Dim...
1
by: davidmcb | last post by:
I have defined an enumerated type that I use in various places in my schema. I am in the process of creating a new complex type that contains an attribute that I want to restrict to NOT being a...
3
by: Jeffrey Grantz | last post by:
I took the code below from an example of FolderBrowserDialog but I keep getting the subject diagnostic on DialogResult.OK and I can't figure out what I am doing wrong. Any Help? ADVthanksANCE
2
by: gomzi | last post by:
hi, I have binded an arraylist to a datagrid. the items in the arraylist are objects of type myclassname. Now, when i try to bind the columns through a template field, i get this error :- No...
8
by: Sep410 | last post by:
Hi all, Here is the code that I have: For h = 2 To dt.Rows.Count - 1 For j = 0 To 11 Excel.Cells(h, k).value = (dt.Rows(h).Item(j).Value).ToString ...
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
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...
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
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,...
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.