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

instantiation of struct with no tag name

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_member = 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_member = hack_instancer; I do:

hack_copy(&st.embedded_member, &h, sizeof(struct hack));

I know that in this case I could just have done:
strut some_struct some_instance;
some_instance.embedded_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
Nov 14 '05 #1
1 3151


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_member = 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_member = hack_instancer; I do:

hack_copy(&st.embedded_member, &h, sizeof(struct hack));

I know that in this case I could just have done:
strut some_struct some_instance;
some_instance.embedded_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.embedded_member = new;
printf("mystruct.embedded_member.a = %d\n",
mystruct.embedded_member.a);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #2

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

Similar topics

4
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...
4
by: ES Kim | last post by:
Please consider this code: void foo(int); template<typename T> struct S { void f() { foo(1); // (1)
6
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...
12
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...
5
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;
6
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...
5
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...
0
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...
11
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 { } ;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...

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.