0

I'm having a bit of a problem creating a ctypes structure with a string in it and initializing it with a meaningful value.

Here's my structure:

class MyStruct( Structure ):
    _fields_ = [ ("someString", c_char_p) ]

and here's me trying to initialize it

obj = MyStruct( "something" )

Both attempts fail of course. Here's the error message:

obj_1= MyStruct( "something" ) TypeError: string or integer address expected instead of str instance

Same thing happens if I use *c_char_p* operator obj = MyStruct( c_char_p( "something" ) )

I must mention that this code is executed in Blender 2.63a environment.

Can anyone help me solve this problem?

Piotr Trochim
  • 693
  • 5
  • 15

1 Answers1

5

You need to use the __init__ method to instantiate:

class MyStruct( Structure ):
    def __init__(self,some_string):
         self._fields_ = [ (some_string, c_char_p) ]

Then to make a new Structure, should work:

obj = MyStruct( "something" )
Community
  • 1
  • 1
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • 1
    @Paksas, you should accept the best answer by selecting the check to the left and upvoting any helpful answers. It's the SO way of rewarding the people that help you. – Mark Tolonen Sep 01 '12 at 04:51