Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define a structure like in C [duplicate]

Tags:

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?

like image 680
Bin Chen Avatar asked Sep 06 '10 00:09

Bin Chen


People also ask

Can we copy struct in C?

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.

How do you copy one structure to another?

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));

What is a copy structure?

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.

How many ways can you define a structure in C?

There are two ways to declare structure variable: By struct keyword within main() function.


1 Answers

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) 
like image 55
Nathan Davis Avatar answered Oct 14 '22 22:10

Nathan Davis