Re: typedef union
"Mark Tolonen" <[email protected]>
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
"Nils Wagner" <[email protected]> wrote in message news:[email protected]... > Hi all, > > I am newbie to ctypes. > > How can I "translate" the following C-code into Python ? > I don't know how to handle HEAD and memcpy ? > Any pointer would be appreciated. > > Thanks in advance. > > Nils > > > typedef union { > int i; > float f; > char c[4]; > } Word; > > int > I,J,K,N,IDE,IA,IE,IERR,JUNIT,JUNCAT,NDATA,NREC,LREADY,ONE=1; > Word BUF[100],HEAD[30]; > > for (I=5;I<LHEAD;I++) > HEAD[I].i = 0; > HEAD[ 0].i = 1; > HEAD[ 1].i = LHEAD + NDATA*7; > HEAD[ 2].i = LHEAD; > HEAD[ 3].i = NDATA; > HEAD[ 4].i = 7; > memcpy (HEAD[ 7].c,"DSIO",4); > memcpy (HEAD[ 8].c,"TEST",4); > memcpy (HEAD[ 9].c,"NPCO",4); > memcpy (HEAD[10].c," ",4); > memcpy (HEAD[11].c,"DSIO",4); > HEAD[20].i = 1; > HEAD[21].i = NDATA; > STRING = "MM RAD"; > DSEINH(STRING,&HEAD[24].i,&ONE, > strlen(STRING)); Here's a rough translation. Note how the arrays are declared. "c_char*4" is a 4-character array *type*. (Word*100)() is a 100-Word array *instance*. To assign to the char array, just use a maximum 4-character string. import ctypes class Word(ctypes.Union): _fields_ = [ ('i',ctypes.c_int), ('f',ctypes.c_float), ('c',ctypes.c_char*4)] I=J=K=N=IDE=IA=IE=IERR=JUNIT=JUNCAT=NDATA=NREC=LREADY=ONE=1 BUF = (Word*100)() HEAD = (Word*30)() # not defined in C code LHEAD = 30 for I in range(5,LHEAD): HEAD[I].i = 0 HEAD[ 0].i = 1; HEAD[ 1].i = LHEAD + NDATA*7 HEAD[ 2].i = LHEAD HEAD[ 3].i = NDATA HEAD[ 4].i = 7 HEAD[ 7].c = "DSIO" HEAD[ 8].c = "TEST" HEAD[ 9].c = "NPCO" HEAD[10].c = " " HEAD[11].c = "DSIO" HEAD[20].i = 1 HEAD[21].i = NDATA STRING = "MM RAD" # Not enough info # DSEINH(STRING,&HEAD[24].i,&ONE,strlen(STRING)); -Mark ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev