Parsing a struct to a function, not a pointer to it

Posted By: Clemens

Parsing a struct to a function, not a pointer to it - 09/14/11 21:46

Sry, I know I've still to learn a lot about structs/pointer/etc. Somewhen I will go through a good book... till then there are questions like the following, for some of you sounding stupid or at least obviously/simple ... anyway:

--------------
this works, native:
Code:
function demonstration(var my_own_var) {
	...
	my_own_var = 37;	// do whatever I want with it, nobody cares
	...
}
...
	var number_for_everyone = 1;
	demonstration(number_for_everyone);
	...
	number_for_everyone = 7;
	demonstration(number_for_everyone);
	...
	demonstration(number_for_everyone);
...



this not:
Code:
function demonstration(ANY_STRUCT my_own_struct) {
	...
	my_own_struct.x = 37;	// do whatever I want with it, nobody cares?
	...
}
...
	ANY_STRUCT struct_for_everyone;
	struct_for_everyone.x = 1;
	demonstration(struct_for_everyone);
...


Compiler: Syntax error: can not push STRUCT@69

Why?

I only know this way round:
Code:
function demonstration(ANY_STRUCT* temp_my_own_struct) {
	ANY_STRUCT my_own_struct;
	my_own_struct.x=temp_my_own_struct.x;
	...
	my_own_struct.x = 37;	// do whatever I want with it, nobody cares! 
	...
}
...
	ANY_STRUCT struct_for_everyone;
	struct_for_everyone.x = 1;
	demonstration(struct_for_everyone);
...



Any better solutions?

At least a possibility to copy the whole dataset of a struct into a new one. Like vec_set but for every struct? (okay, I could write an own function or define for it...)

Regards,
Clemens
Posted By: tzw

Re: Parsing a struct to a function, not a pointer to it - 09/14/11 22:08

passing pointers maybe for performance reasons.
using memcpy for coping a memory field to another byte by byte.
you can look into "c standard lib" for details

Posted By: Joozey

Re: Parsing a struct to a function, not a pointer to it - 09/14/11 22:13

You can give basic variables like int, float, var, char, ... as function arguments, but not whole structures as they are too large to pass. I'm not sure about the specifics, but I know that in real C, execution will have a considerable impact on speed when passing large amounts of bytes as argument. You are supposed to pass pointers only. And if you need to copy a structure, either create a copy structure function, or use this generic definition:

Code:
#define clone_struct( source, type ) memcpy( sys_alloc( sizeof( type ) ), source, sizeof( type ) )
...
Type* source_struct = sys_alloc( sizeof( Type ) );
Type* cloned_struct = clone_struct( source_struct, Type );


Not extensively tested but it should work...

Pay attention when you use this though. You might actually need a more specific copy_object function. If a struct contains pointers to other structures that you also need to have copied, its best to create your own copy_object function that does this.
Posted By: Clemens

Re: Parsing a struct to a function, not a pointer to it - 09/14/11 22:27

I see, it's just the size... okay, doesn't make that much sense for me, because it has to be created anyway, furthermore I also can parse ~unlimited (?) var parameters - but okay!
Thanks for these quick answers!! Going to try that generic definition. thx!
Posted By: Clemens

Re: Parsing a struct to a function, not a pointer to it - 09/14/11 22:35

Just to go sure, your source implemented in mine above...
should look like this:

Code:
#define clone_struct( source, type ) memcpy( sys_alloc( sizeof( type ) ), source, sizeof( type ) )

function demonstration(ANY_STRUCT* temp_my_own_struct) {
	ANY_STRUCT* temp_my_own_struct = sys_alloc( sizeof( ANY_STRUCT ) );
	ANY_STRUCT* my_own_struct = clone_struct( temp_my_own_struct, ANY_STRUCT );
	...
	my_own_struct.x = 37;	// do whatever I want with it, nobody cares! 
	...
}
...
	ANY_STRUCT struct_for_everyone;
	struct_for_everyone.x = 1;
	demonstration(struct_for_everyone);
...



Okay, I'm going to test it... even I'm still not happy about that quite long solution... maybe I'll write a specific function/define for the struct.

Or does anybody know a more professional way?
Posted By: TechMuc

Re: Parsing a struct to a function, not a pointer to it - 09/14/11 22:49

eine einfache allokation reicht laugh dafür steht das sys_alloc ja im präprozessor

Code:
function demonstration(ANY_STRUCT* temp_my_own_struct) {
	ANY_STRUCT* copy = clone_struct( temp_my_own_struct, ANY_STRUCT );
	......
        copy->x = 12;
        sys_free(copy);
}


Posted By: TechMuc

Re: Parsing a struct to a function, not a pointer to it - 09/14/11 22:52

oh english.. hm..

please get used to use "->" instead of "." (or better said, use them both, in the right context.. -> for pointer, "." for values.. your code is really hard to read this way, and it will for sure not help you to learn those things faster if you rely on the shitty lite-c compiler to resolve such things )
Posted By: Clemens

Re: Parsing a struct to a function, not a pointer to it - 09/14/11 23:43

cool, that works... of course it has to be sys_malloc

yeah, I'm agree with the "->"/"." critic! relating to me and to the lite-c compiler wink
(would be easier to learn if the compiler would't do it on its own... btw, can it always know the right way? so does se of ./-> defacto make no difference, and liteC just uses this fact to make it easier for the programer?)
Posted By: Myrkling

Re: Parsing a struct to a function, not a pointer to it - 09/15/11 07:57

You can use #define PRAGMA_POINTER to inhibit lite-C from detecting pointers automatically.

Just insert this line in your main script, right after the default includes.
Posted By: Clemens

Re: Parsing a struct to a function, not a pointer to it - 09/15/11 08:27

very good hint! thank you
© 2024 lite-C Forums