how to change pointer of a struct in a struct?

Joined
Apr 9, 2010
Messages
3
Reaction score
0
Hi there!

I have to use strange structs that looks like this:

Code:
typedef struct Any
{
	int length;
	char value[1]; /* first element of the array */
} Any;

typedef struct Container
{
	/* ... some other structs ...*/
	Any a_any;
	/* ... some other structs ...*/
} Container;

As everyone can see, it seems to be a choice of design that the size of the struct "Any" has to be determined in run-time. But the problem is that the container struct carries that struct somewhere in the middle so I cannot use the workaround allocating enough memory for the container struct because I would overwrite the following components.

But on the other hand I am neither able to reallocate nor to move the pointer to the "Any" struct to a seperate, previously allocated "Any" struct.

Please take a look at my sample code and give me a hint. I am stuck here... :(

Code:
static const char text[] = {
	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39
};

int _tmain(int argc, _TCHAR* argv[])
{

	Container* c = (Container*) malloc(sizeof(Container));
	Any* any = (Any*) malloc(sizeof(Any)+100);

	any->length = 10;
	memcpy(&any->value, text, 10);

	&c->a_any = any; // I want to do sth. like this...

	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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top