I am going to define a structure and pass it into a function:
In C:
struct stru { int a; int b; }; s = new stru() s->a = 10; func_a(s);
How this can be done in Python?
If the structures are of compatible types, yes, you can, with something like: memcpy (dest_struct, source_struct, sizeof (*dest_struct)); The only thing you need to be aware of is that this is a shallow copy.
Solution 1. With a structure, you can just do a memory copy. Be aware that both structures must have the same layout. memcpy(&cd->ks1, &ks1, sizeof(cd->ks1)); memcpy(&cd->ks2, &ks2, sizeof(cd->ks2));
Structure copies are done the same way no matter how you indicate the structures. For example, PairOfInts *p, *q; p = new PairOfInts; q = new PairOfInts; p->a = 20; p->b = 40; *q = *p; copies structure *p into *q.
There are two ways to declare structure variable: By struct keyword within main() function.
Unless there's something special about your situation that you're not telling us, just use something like this:
class stru: def __init__(self): self.a = 0 self.b = 0 s = stru() s.a = 10 func_a(s)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With