Hi everyone,
I am in a situation where I need to instantiate a struct which has no tag
name:
struct some_struct {
struct {
int a;
} embedded_member ;
};
I need to instantiate the embedded member in my application as a variable
it self - however that is not possible since there is no tag name for this
struct. Therefore I need to be able to do something like this
/* Struct with the same structure as the struct with no tag name */
struct hack {
int a;
} hack_instance;
struct some_struct st;
st.embeddeb_mem ber = hack_instance;
However this gives an type error.
The solution I came up with is the following:
void hack_copy(void *dst, void *src, int size) {
memcpy(dst, src, size);
}
And then instead of st.embeddeb_mem ber = hack_instancer; I do:
hack_copy(&st.e mbedded_member, &h, sizeof(struct hack));
I know that in this case I could just have done:
strut some_struct some_instance;
some_instance.e mbedded_member = 6;
However my application is a an application generating C code, and it would
be a problem to generate that sort of code (loong story :) - the bottom
line is if this trick is possible the design of my application stays
very nice).
So my question is if this is a propper solution? I know that the type
struct hack, is a different type than the embedded struct, however they
should have the same representation in memory? The only thing I am in
doubt of is if there could be some alignment problems in some cases is the
structs contains different datatypes?
Cheers,
--
Michael Birkmose - stud.polyt
Aalborg University - Department of Computer Science
Fredrik Bajers Vej 7, B1-215 1 3187
Michael Birkmose wrote: Hi everyone,
I am in a situation where I need to instantiate a struct which has no tag name:
struct some_struct { struct { int a; } embedded_member ; };
I need to instantiate the embedded member in my application as a variable it self - however that is not possible since there is no tag name for this struct. Therefore I need to be able to do something like this
/* Struct with the same structure as the struct with no tag name */ struct hack { int a; } hack_instance;
struct some_struct st; st.embeddeb_mem ber = hack_instance;
However this gives an type error. The solution I came up with is the following:
void hack_copy(void *dst, void *src, int size) { memcpy(dst, src, size); }
And then instead of st.embeddeb_mem ber = hack_instancer; I do:
hack_copy(&st.e mbedded_member, &h, sizeof(struct hack));
I know that in this case I could just have done: strut some_struct some_instance; some_instance.e mbedded_member = 6;
However my application is a an application generating C code, and it would be a problem to generate that sort of code (loong story :) - the bottom line is if this trick is possible the design of my application stays very nice).
So my question is if this is a propper solution? I know that the type struct hack, is a different type than the embedded struct, however they should have the same representation in memory? The only thing I am in doubt of is if there could be some alignment problems in some cases is the structs contains different datatypes?
The hack may work but it is a violation of C Standard.
Two structs with different tag names are two distinct types.
The standard states that the two structs in the simple assignment,
using the assignment operator, '=', must be of the same type.
Why don't you simply add a struct data to the inner struct in the
definition. I don't see how that will affect the already written
code.
#include <stdio.h>
struct some_struct {
struct inner_struct{
int a;
} embedded_member ;
};
int main(void)
{
struct some_struct mystruct;
struct inner_struct new;
new.a = 56;
mystruct.embedd ed_member = new;
printf("mystruc t.embedded_memb er.a = %d\n",
mystruct.embedd ed_member.a);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email) http://www.geocities.com/abowers822/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: C. Carbonera |
last post by:
/*
Hi,
I have a problem with explicit instantiation of templates in Visual
C++ 6.0.
I have provided the source below. I have an example of a function
template that produces incorrect output in the code below.
The function
template < typename T >
|
by: ES Kim |
last post by:
Please consider this code:
void foo(int);
template<typename T>
struct S
{
void f()
{
foo(1); // (1)
|
by: Thomas Maier-Komor |
last post by:
Hi everybody,
I am a little bit confused with the syntax of explicit instantiation,
and I am not sure that it is possible to do what I want to do.
Maybe someone has an idea.
Consider a template class:
template <class ABC>
struct CT : public ABC
|
by: mlimber |
last post by:
This is a repost (with slight modifications) from
comp.lang.c++.moderated in an effort to get some response.
I am using Loki's Factory as presented in _Modern C++ Design_ for
message passing in an embedded environment with multiple processors. I
created a policy for classes, which, I had hoped, would automatically
register the class with the appropriate factory:
// In some header file...
#include <cassert>
|
by: Rich |
last post by:
Suppose the following:
typedef void (*funcptrs)(void);
typedef struct {
unsigned int *in;
unsigned int *out;
unsigned int *overrun;
funcptrs myptrs; /* >=1 for ansi */
} yada;
| |
by: Kaba |
last post by:
Here is a short snippet of code that does not compile (tested with Vc8
and Comeau). This is because somehow Vector<0gets instantiated, for
which the array size goes to 0. However, I don't quite get it why it
gets instantiated in the first place. So why does not this compile?
template <int N>
struct Vector
{
int data_;
};
|
by: dascandy |
last post by:
The following program produces no warnings, no errors, but doesn't do
what I expect it to. I expect it to produce a warning or error. What
does the standard say about this and/or what should it say? Stroustrup
mentions it's an error (The C++ programming language, p343) but
doesn't say whether the standard says so or whether it's
implementation defined.
Both GCC and MSVC return "4 4 40", showing they instantiated the
template at the...
|
by: greek_bill |
last post by:
Hi,
I have a template function for which I use SFINAE to restrict one of
the parameters. Then I also have a partial specialization of this
function.I would like to provide an explicit instantiation of the
partially specialized version, but my compiler (VC8) complains because
it fails the SFINAE version.
I just realized as I was typing this that I'm using partial
_function_ specialization. I'm sure I remember reading somewhere that
|
by: sgurukrupagmailcom |
last post by:
Hi,
When searching for a solution to my problem I stumbled upon 'Curiously Recurring Template Pattern' see link.
This is how the pattern looks:
template < typename T >
struct y
{
} ;
|
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...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |