Pass a string as a char pointer array using ctypes

steve

I have a C function of this signature:

int fileopen(const char *fname, int flags)

In Linux system, I want to pass the fname from Python using ctypes so I use ctypes.c_char_p("file1.dat") as the following:

import ctypes as ctypes

dll_name = "IO/pyaio.so"
dllabspath = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + dll_name
pyaio = ctypes.CDLL(dllabspath)

fd_w = pyaio.fileopen(ctypes.c_char_p("file1.dat"), 1)

But I get this error:

Traceback (most recent call last):
  File "test.py", line 21, in <module>
    fd_w = pyaio.fileopen(ctypes.c_char_p("file1.dat"), 1) # 0 read, 1 write
TypeError: bytes or integer address expected instead of str instance

I don't know another way to pass the string"file1.dat" than using ctypes.c_char_p. How to solve this problem?

Thank you

fferri

Change "file1.dat" to b"file1.dat" (which is the same you would get with 'file1.dat'.encode('ascii')).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass a Pointer of String-Array in a function using Ctypes?

From Dev

ctypes: decomposing/casting pointer to char into array of pointer to char

From Dev

Pass char pointer array to function

From Dev

Pass char pointer/array to a function

From Dev

Pass parameter of type pointer to pointer to a C function using ctypes

From Dev

Ctypes pointer on element in ctypes array

From Dev

How to produce a character array in ctypes and pass its pointer as a function argument

From Dev

Modify a character pointer string array (char * string[])

From Dev

Build a string (char pointer) from an integer array

From Dev

Converting python string object to c char* using ctypes

From Dev

pointer to char or char array

From Dev

Pointer to char into char array

From Dev

Is it applicable to pass pointer to character (after assigning string to it) to method and modify this string after without using array and why not?

From Dev

How to initialize string from array of char pointer values (char* ptr[])

From Dev

How to store a string that the user inputted into a char pointer instead of a char array

From Dev

Read a string char by char from an associative pointer array

From Dev

Pass string array as argument with function pointer

From Dev

ctypes string pointer type error

From Dev

Pass a “char *“ pointer to a function

From Dev

Returning string from C++ function with ctypes gives large int, not char pointer

From Dev

Pass byte numpy array to C function using ctypes

From Dev

C++ pass const char* pointer array to object

From Dev

Displaying a char pointer using similar syntax as a char array?

From Dev

Using char array instead of String

From Dev

Pass array with pointer, using if (? operator) statement

From Dev

How to assign 2D string array to char pointer array?

From Dev

C++ How to convert char pointer array to string array?

From

What is proper Ctypes to pass array

From Dev

Comparison of char array and char pointer

Related Related

  1. 1

    How to pass a Pointer of String-Array in a function using Ctypes?

  2. 2

    ctypes: decomposing/casting pointer to char into array of pointer to char

  3. 3

    Pass char pointer array to function

  4. 4

    Pass char pointer/array to a function

  5. 5

    Pass parameter of type pointer to pointer to a C function using ctypes

  6. 6

    Ctypes pointer on element in ctypes array

  7. 7

    How to produce a character array in ctypes and pass its pointer as a function argument

  8. 8

    Modify a character pointer string array (char * string[])

  9. 9

    Build a string (char pointer) from an integer array

  10. 10

    Converting python string object to c char* using ctypes

  11. 11

    pointer to char or char array

  12. 12

    Pointer to char into char array

  13. 13

    Is it applicable to pass pointer to character (after assigning string to it) to method and modify this string after without using array and why not?

  14. 14

    How to initialize string from array of char pointer values (char* ptr[])

  15. 15

    How to store a string that the user inputted into a char pointer instead of a char array

  16. 16

    Read a string char by char from an associative pointer array

  17. 17

    Pass string array as argument with function pointer

  18. 18

    ctypes string pointer type error

  19. 19

    Pass a “char *“ pointer to a function

  20. 20

    Returning string from C++ function with ctypes gives large int, not char pointer

  21. 21

    Pass byte numpy array to C function using ctypes

  22. 22

    C++ pass const char* pointer array to object

  23. 23

    Displaying a char pointer using similar syntax as a char array?

  24. 24

    Using char array instead of String

  25. 25

    Pass array with pointer, using if (? operator) statement

  26. 26

    How to assign 2D string array to char pointer array?

  27. 27

    C++ How to convert char pointer array to string array?

  28. 28

    What is proper Ctypes to pass array

  29. 29

    Comparison of char array and char pointer

HotTag

Archive