yannickperrenet / strman

🤔 (Educational) Calling C from Python using ctypes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling C from Python using ctypes

# In case you want to run the executable: ./strman
gcc -o strman strman.c

# Make sure to run this if you update the strman.c file!
gcc -fPIC -shared -o strman.so strman.c

python3 example.py

Produces the output:

Python answer: ['Hello,', 'world!']
C answer: ['Hello,', 'world!']

Project context

I was toying around with C to better understand memory allocation and felt like implementing string splitting (e.g. split "Hello, world!" on " " gives "Hello," and "world!") would be a good first project. Then after implementing it, I figured why don't I try to call this function from Python. I might as well learn something new that I can use that knowledge again in the future to optimize some Python code.

About

🤔 (Educational) Calling C from Python using ctypes

License:Other


Languages

Language:C 59.5%Language:Python 40.5%