473,324 Members | 1,856 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,324 software developers and data experts.

Is this valid C?

Is the following code valid C?

typedef struct
{
char x[128];
} foo;

typedef struct
{
char sameSizeAsFooX[ sizeof ((foo *)0)->x ];
} bar;
I'd like to use this construct to ensure keeping member sizes in two
structures in lockstep but I'm not sure if it's really portable.

Thanks,
Andrew
Jun 6 '06 #1
4 1541


andrew queisser wrote On 06/06/06 13:24,:
Is the following code valid C?

typedef struct
{
char x[128];
} foo;

typedef struct
{
char sameSizeAsFooX[ sizeof ((foo *)0)->x ];
} bar;
I'd like to use this construct to ensure keeping member sizes in two
structures in lockstep but I'm not sure if it's really portable.


It looks all right to me. I think I'd prefer to
see it done differently, though:

#define XCOUNT 128

typedef struct {
char x[XCOUNT];
} foo;

typedef struct {
char sameSizeAsFooX[XCOUNT];
} bar;

A better solution (sometimes) would be to wrap the
common elements in their own substructure:

typedef struct {
char x[128];
} xstuff;

typedef struct {
double trouble;
xstuff boo;
double scotch;
} foo;

typedef struct {
xstuff booboo, bluegoo, zoocrew;
short snort;
} bar;

Much depends on your reasons for wanting to keep
these things' sizes synchronized.

--
Er*********@sun.com

Jun 6 '06 #2

"Eric Sosman" <Er*********@sun.com> wrote in message
news:1149615849.508951@news1nwk...


andrew queisser wrote On 06/06/06 13:24,:
Is the following code valid C?

typedef struct
{
char x[128];
} foo;

typedef struct
{
char sameSizeAsFooX[ sizeof ((foo *)0)->x ];
} bar;
I'd like to use this construct to ensure keeping member sizes in two
structures in lockstep but I'm not sure if it's really portable.


It looks all right to me. I think I'd prefer to
see it done differently, though:

#define XCOUNT 128

typedef struct {
char x[XCOUNT];
} foo;

typedef struct {
char sameSizeAsFooX[XCOUNT];
} bar;

Yes, this is what I normally do (and of course I would use a #define inside
foo anyway) but the problem is that I might change my mind about the size of
foo->x and then I have to search for all occurrences of the #define. With
this new style I can see that my intent was to force one array to be the
same as another one. With the #define the link between the two arrays is not
clear in isolation.

Thanks for the comments,
Andrew
Jun 7 '06 #3

"andrew queisser" <an***************@hp.com> wrote in message
news:Ky****************@news.cpqcorp.net...

"Eric Sosman" <Er*********@sun.com> wrote in message
news:1149615849.508951@news1nwk...


andrew queisser wrote On 06/06/06 13:24,:
Is the following code valid C?

typedef struct
{
char x[128];
} foo;

typedef struct
{
char sameSizeAsFooX[ sizeof ((foo *)0)->x ];
} bar;
I'd like to use this construct to ensure keeping member sizes in two
structures in lockstep but I'm not sure if it's really portable.
It looks all right to me. I think I'd prefer to
see it done differently, though:

#define XCOUNT 128

typedef struct {
char x[XCOUNT];
} foo;

typedef struct {
char sameSizeAsFooX[XCOUNT];
} bar;

Yes, this is what I normally do (and of course I would use a #define
inside foo anyway) but the problem is that I might change my mind about
the size of foo->x and then I have to search for all occurrences of the
#define. With this new style I can see that my intent was to force one
array to be the same as another one. With the #define the link between the
two arrays is not clear in isolation.


All occurrences of the #define? I surely hope you would only #define it
once!
If you have multiple files, put the #define in an include file that is
included in all of the source files.

If the macro name is chosen meaningfully, it is perfectly clear about the
size and what it means: the same size as anything declared using XCOUNT.
Or name it FOO_XCOUNT if you want to point out that 'foo' is the
primary driver.

Thanks for the comments,
Andrew

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Jun 7 '06 #4
>> Yes, this is what I normally do (and of course I would use a #define
inside foo anyway) but the problem is that I might change my mind about
the size of foo->x and then I have to search for all occurrences of the
#define. With this new style I can see that my intent was to force one
array to be the same as another one. With the #define the link between
the two arrays is not clear in isolation.
All occurrences of the #define? I surely hope you would only #define it
once!
If you have multiple files, put the #define in an include file that is
included in all of the source files.


Right, #define once, in one header file.
If the macro name is chosen meaningfully, it is perfectly clear about the
size and what it means: the same size as anything declared using XCOUNT.
Or name it FOO_XCOUNT if you want to point out that 'foo' is the
primary driver.


That goes a long way toward keeping things organized. However, if I go back
to a set of source files I haven't worked on in a long time I might decide
that foo->x really needs to be a different size, perhaps based on a
different #define (e.g. maybe it should be exactly MAX_PATH+1 or something
like that.) I could now #define FOO_XCOUNT to be MAX_PATH+1 but I'd really
like to see the meaningful MAX_PATH+1 in the array declaration so at that
point I have to grep through my sources to find out who else is using
FOO_XCOUNT. With this new construct I'm using it's automatic.

Andrew


Jun 8 '06 #5

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

Similar topics

12
by: lawrence | last post by:
I have a string which I want to send to eval(). How can I test it ahead of time to make sure it is valid code? I don't want to send it to eval and get parse errors. I want to do something like...
16
by: siliconmike | last post by:
Hi, I'm looking for a reliable script that would connect to a host and somehow determine whether an email address is valid. since getmxrr() only gets the mx records.. Links/pointers ? Mike
1
by: Anna | last post by:
Hi all. I have probably a rather stupid question. If there is an HTML document, XML-formed using JTidy, is there any tool to convert it to valid XHTML? I.e. so that all the tags and attribute...
7
by: JR | last post by:
Hey all, I have read part seven of the FAQ and searched for an answer but can not seem to find one. I am trying to do the all too common verify the data type with CIN. The code from the FAQ...
23
by: James Aguilar | last post by:
Someone showed me something today that I didn't understand. This doesn't seem like it should be valid C++. Specifically, I don't understand how the commas are accepted after the function...
3
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
1
by: Robert Morgan | last post by:
|I'm trying to run a query on a database using php and postgres functions ||<?php db_connect(); $stat = pg_exec($connstr,"SELECT WSID from tblWorkstation "); while ($row = pg_fetch_rows($stat))...
1
by: illegal.prime | last post by:
Hey all, I have an app, that could take two numbers of any type of numerical type int, long, double, float, uint, ulong, etc. I want to check that the numbers are part of a range that I consider...
10
by: SpreadTooThin | last post by:
Hi I'm writing a python script that creates directories from user input. Sometimes the user inputs characters that aren't valid characters for a file or directory name. Here are the characters...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.