On 4/19/2009 10:50 PM, Dag Sverre Seljebotn wrote:

> Could we have some kind of keyword to define packed structs in Cython?
> This is not part of the C standard, but is supported in most C compilers
> through extensions. 

Since it's not in the standard, there is no standard syntax for this.

But you can fake it using a char array and some constants storing offsets.

    /*
    cdef packed struct MyStruct:
       char a
       double b
    */
    const int mystruct_size = sizeof(char)+sizeof(double);
    const int mystruct_offset_a = 0;
    const int mystruct_offset_b = sizeof(char);

    /*
    cdef MyStruct mystruct
    */
    char mystruct[mystruct_size]; /* sizeof(char) is 1 by defintion */


    /*
    cdef char a
    cdef double b
    a = mystruct.a
    b = mystruct.b
    */
    char a;
    double b;
    a = *(char *)((char *)mystruct + mystruct_offset_a);
    b = *(double *)((char *)mystruct + mystruct_offset_b);



Sturla Molden

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to