instantiation of struct with no tag name

M

Michael Birkmose

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,
 
A

Al Bowers

Michael said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top