Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to change pointer of a struct in a struct?

Reply
Thread Tools

how to change pointer of a struct in a struct?

 
 
Frighty Frighty is offline
Junior Member
Join Date: Apr 2010
Posts: 3
 
      04-13-2010
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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Struct pointer vs. struct array pointer aleksa C Programming 16 02-20-2013 08:20 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
(: Pointer to struct withing pointer to struct :) Zero C Programming 16 11-19-2005 01:27 AM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57