473,594 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning common field of multiple structs

Hello all,
I wonder if there's a way to obtain a given field from different structs.
Something like this:

struct sDigital {
char Label[255];
int Something;
} tDigital;

struct sAnalog {
double SomethingElse;
char Label[255];
} tAnalog;

char *GetLabel(void* DigOrAnal) {
return DigOrAnal->Label;
}

I know that doesn't work and I have to add a type variable indicator and a
slew of 'if' tests. But I wonder if there are some simplifying tricks.
--
Guillaume Dargaud
http://www.gdargaud.net/
Nov 21 '08 #1
4 1685
Guillaume Dargaud wrote:
Hello all,
I wonder if there's a way to obtain a given field from different structs.
Something like this:

struct sDigital {
char Label[255];
int Something;
} tDigital;

struct sAnalog {
double SomethingElse;
char Label[255];
} tAnalog;

char *GetLabel(void* DigOrAnal) {
return DigOrAnal->Label;
}

I know that doesn't work and I have to add a type variable indicator and a
slew of 'if' tests. But I wonder if there are some simplifying tricks.
Not that I can think of. If you're free to rearrange the
structs so you can put Label first, it's easily solved -- but
as shown, I think you're out of luck. No "MOVE CORRESPONDING"
in C, I'm afraid.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 21 '08 #2
Guillaume Dargaud wrote:
Hello all,
I wonder if there's a way to obtain a given field from different structs.
Something like this:

struct sDigital {
char Label[255];
int Something;
} tDigital;

struct sAnalog {
double SomethingElse;
char Label[255];
} tAnalog;

char *GetLabel(void* DigOrAnal) {
return DigOrAnal->Label;
}

I know that doesn't work and I have to add a type variable indicator and a
slew of 'if' tests. But I wonder if there are some simplifying tricks.
Yes; the simplifying trick is not to fall into this trap. If you find that
it's happening, /stop/ and think about what it implies about your design.

The most obvious fix is to use a struct with a Label field (which might
usefully be `char*`, not `char[]`, depending) and a union of sDigital
and sAnalog. For that you might want your type indicator and if-stew.
But at least you've got rid of one tripwire.

(I say "stew" not "slew" in homage to the signature.)

--
"It is seldom good news." ~Crystal Ball~, /The Tough Guide to Fantasyland/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Nov 21 '08 #3
On Nov 21, 6:13*am, "Guillaume Dargaud"
<use_the_form_o n_my_contact_p. ..@www.gdargaud .netwrote:
Hello all,
I wonder if there's a way to obtain a given field from different structs.
Something like this:

struct sDigital {
*char Label[255];
*int Something;

} tDigital;

struct sAnalog {
*double SomethingElse;
*char Label[255];

} tAnalog;

char *GetLabel(void* DigOrAnal) {
* * return DigOrAnal->Label;

}

I know that doesn't work and I have to add a type variable indicator and a
slew of 'if' tests. But I wonder if there are some simplifying tricks.
--
Guillaume Dargaudhttp://www.gdargaud.ne t/
Chris Dollin's suggestion is the right one; redesign your types so
that you have a single struct type with variant members for analog and
digital:

typedef struct sComm
{
char Label[255];
union {
tdigital digitalStuff;
tanalog analogStuff;
} digitalOrAnalog ;
} tComm;

Having said that, here's one trick I've used in the past, and I think
it's applicable generally (but don't take my word for it). The
address of the first member of a struct object is the same as the
address of the struct object itself. You could rearrange your struct
definitions so that Label is the first member of both:

typedef struct sDigital
{
char Label[255];
...
} tDigital;

typedef struct sAnalog
{
char Label[255];
...
} tAnalog;

That way, a pointer to the struct can easily be cast into a pointer to
the Label buffer:

tDigital d;
char *dlabel = (char *) &d; // dlabel now points to d.Label
tAnalog a;
char *alabel = (char *) &a; // alabel now points to a.Label

Obviously, that's a bit ugly and non-intuitive, which is why you
should go with Chris' suggestion.
Nov 21 '08 #4
Guillaume Dargaud wrote:
I wonder if there's a way to obtain a given field from different structs.
Something like this:

struct sDigital {
char Label[255];
int Something;
} tDigital;

struct sAnalog {
double SomethingElse;
char Label[255];
} tAnalog;

char *GetLabel(void* DigOrAnal) {
return DigOrAnal->Label;
}

I know that doesn't work and I have to add a type variable indicator and a
slew of 'if' tests. But I wonder if there are some simplifying tricks.
Give the above type definitions, there are no "simplifyin g tricks"
besides a simple macro

#define GetLabel(DigOrA nal) ((DigOrAnal)->Label)

If you can change the types, the alternative approach would be to
simulate a C++-like class hierarchy

struct sLabeled {
char Label[255];
};

struct sDigital {
sLabeled Label;
int Something;
} tDigital;

STATIC_ASSERT(o ffsetof(sDigita l, Label) == 0);

struct sAnalog {
sLabeled Label;
double SomethingElse;
} tAnalog;

STATIC_ASSERT(o ffsetof(sAnalog , Label) == 0);

char *GetLabel(void* DigOrAnal) {
/* assert(<make sure it is really safe>); */
return ((sLabeled*) DigOrAnal)->Label;
}

...
sDigital d;
sAnalog a;
...
char *dl = GetLabel(&d);
char *al = GetLabel(&a);

Of course, it is a good idea to put a bunch of safeguard into this
dangerous code, which will perform a run-time check of the data passed
to 'GetLabel' at least in "debug" configuration of the program (like add
a signature to 'sLabeled').

--
Best regards,
Andrey Tarasevich
Nov 21 '08 #5

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

Similar topics

6
42191
by: Samuel Hon | last post by:
Hi I'm not sure what the best approach for this is: I have a stored procedure which I would like to use to return several output values instead of returning a recordset. CREATE PROCEDURE Test (@param1 int, @param2 int OUTPUT, @param3 int OUTPUT) AS SELECT field2, field3 FROM Table WHERE field1 = @param1
11
3459
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I tend to iterate over subsets of these arrays by walking a pointer and an offset per dimension across their dimensions. I've found that this tends to result in more performance in a portable manner than doing explicit indexing calculations and...
4
1547
by: Adrian20XX | last post by:
Hi, Hi, I have a typedefed struct, and later when I declare multiple const structures and want to use a field of one in an inialization, I get the following error: "error C2099: initializer is not a constant". typedef struct { int Start; int attribute; } MyStructure ; #define FIRST 1
160
5811
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
7954
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
7882
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
8377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8016
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5415
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3905
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
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
1
1487
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1218
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.