Gamestudio Links
Zorro Links
Newest Posts
Historical ticks for Lookback backfill
by Zheka. 07/08/20 09:10
Trade Mode without a broker
by jj02520024. 07/08/20 01:20
How to handle algo name in Simulate.c
by happysun. 07/07/20 08:56
Erasing a datapoint in a plot array
by jcl. 07/06/20 09:51
typo
by jcl. 07/06/20 09:44
Video Panel System
by Emre. 07/06/20 09:39
Market Internals Data
by Spirit. 07/05/20 07:50
AUM Magazine
Latest Screens
The Space Between
Pogostuck: Rage With Your Friends
Worst Case Z
AckCon'18 - Lotter vs the World 2 - Preview Release
Who's Online Now
8 registered members (3run, alibaba, Zheka, kvm, Ayumi, 20BN, jenGs, 1 invisible), 368 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
TraderKevin, deus777, carolthesecond, Roger399, vrindger
18474 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Nested structs #478707
11/30/19 15:06
11/30/19 15:06
Joined: Dec 2014
Posts: 173
Germany
Smon Offline OP
Member
Smon  Offline OP
Member

Joined: Dec 2014
Posts: 173
Germany
Background: I'm working on an indicator in R that's so extremely slow I have to implement a cache. My indicator gives back a struct (inner). My cache uses a struct (outer) containing the price boundaries, for which the indicator would return the exact same value (fortunately this is the case!).

I want to use nested structs in order to not have to copy the inner struct one element at a time. I also don't want to have to adapt the outer struct if I make changes to the inner struct.


I think I must use memcpy. The manual tells us:

Quote
memcpy(void* dest, void* src, long size)
Copies size bytes from src to dest. Can be used to copy a whole struct or array with a single function.

Link


This is some demo code:

Code
function main()
{
	typedef struct
	{
		int inside_element;
	} INNER;
	typedef struct
	{
		int outside_element;
		INNER nested;
	} OUTER;
	INNER inner;
	OUTER outer;
	inner.inside_element = 42; //prepare the element to get nested
	outer.outside_element = 13; //some element in the outer struct
	outer.nested = inner; //try to store the inner struct into the outer struct
	//Error in 'line 21: 
	//Syntax error: Wrong type EQU:STRUCT@16::STRUCT@16
	//memcpy(outer.nested, inner, sizeof(inner)); //tried something. Not working when declaring inner and outer as pointers either
	INNER test = outer.nested; //read back the nested struct
	printf("\nnested element: %d; other element: %d", test.inside_element, outer.outside_element);
}


I'm curious about the solution! I hope somebody can help.

Re: Nested structs [Re: Smon] #478709
11/30/19 17:09
11/30/19 17:09
Joined: Jun 2007
Posts: 1,323
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,323
Hiporope and its pain
Hi,
'memcpy' espects memory addresses. You need to use the reference operator (&) in order to get a struct address.

Code
memcpy(&outer.nested, &inner, sizeof(INNER));


Anyway, you can't assign structs directly
Code
outer.nested = inner; // error
...
INNER test = outer.nested; // error


Depending on how you do receive the INNER struct, you can perform two actions: memcpy, as explained, or save the reference of the incoming struct. Something like this:
Code
typedef struct
{
	int inside_element;
} INNER;
typedef struct
{
	int outside_element;
	INNER * nested; // a pointer to a INNER struct
} OUTER;
...
outer.nested = &inner; //reference!
...

printf("\nnested element: %d; other element: %d", outer.nested->inside_element, outer.outside_element);
// or
INNER * test = outer.nested;
printf("\nnested element: %d; other element: %d", test->inside_element, outer.outside_element);


Hope it helps.
Salud!

Re: Nested structs [Re: txesmi] #478710
11/30/19 18:24
11/30/19 18:24
Joined: Dec 2014
Posts: 173
Germany
Smon Offline OP
Member
Smon  Offline OP
Member

Joined: Dec 2014
Posts: 173
Germany
Thank you very much!


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1