Created
May 16, 2017 11:08
-
-
Save raghavrv/c91d23e490aeb810e3354f6cc0951107 to your computer and use it in GitHub Desktop.
Advance a pointer by sizeof(struct) for a struct member in an array of struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%cython | |
# cython: wraparound=False | |
# cython: boundscheck=False | |
# cython: cdivision=True | |
# cython: nonecheck=False | |
# cython: profile=False | |
cimport numpy as np | |
import numpy as np | |
# Type for pointer addresses | |
from libc.stdint cimport uintptr_t | |
cdef struct Foo: | |
short a | |
# short e | |
# int a | |
# double d | |
double b | |
size_t c | |
cdef Foo[5] foos | |
cdef double* member_ptr | |
member_ptr = <double*> (&foos[0].b) | |
print(<uintptr_t>member_ptr) | |
cdef int i | |
# convert from bits to bytes for addition... | |
cdef uintptr_t size_of_Foo = sizeof(Foo) / 8 | |
print(size_of_Foo) | |
for i in range(5): | |
member_ptr[0] = i + 10 | |
member_ptr += size_of_Foo | |
print(foos) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment