From a book that programmers can learn ... (Python): Pointer

This time it's a pointer operation, so To use pointers in python, I could write code in Python without thinking about it. I haven't studied much, but I will describe what I have investigated.

After a lot of research, I found something called ctypes — external function library for Python. I was grateful to see this because the tutorial was in Japanese, but since it was a usage method on Windows, I was looking for various ways to use it on Mac stacoverflow 11554355 / mac-os-x-lion-python-ctype-cdll-error-lib-so-6-image-not-found).

I tried using it in my own environment, so I will describe it.

>>> from ctypes import *
>>> cdll.LoadLibrary("libc.dylib")
<CDLL 'libc.dylib', handle 7fff6903da98 at 0x1052f6fd0>
>>> libc = CDLL("libc.dylib")
>>> printf = libc.printf
>>> printf(b"Hello, %s\n", b"World!")
Hello, World!
14
>>> strchr = libc.strchr
>>> strchr(b"abcdef", ord("d"))
87110915
>>> 

As an important issue

Create append () function

It was that, The following answer by manipulating the array C ++ code

void append(arrayString& s, char c){
    int oldLength = 0;
    while(s[oldLength] != 0) {
        oldLength++;
    }
    arrayString newS = new char[oldLength + 2];
    for(int i = 0; i < oldLength; i++) {
        newS[i] = s[i];
    }
    newS[oldLength] = c;
    newS[oldLength + 1] = 0;
    delete[] s;
    s = newS;
}

My Python code
#!/usr/bin/env python
#coding:utf-8

#ctypes module(for mac)
from ctypes import *
#cdll.LoadLibrary("libc.dylib")
#libc = CDLL("libc.dylib")

def add(s,c):
    print('Previous string:',list(s))
    print('Previous pointer:', c_wchar_p(s))
    newlength = len(s)+2
    newS = []
    for i in s:
        newS += i
    for j in c:
        newS += j
    print('After string:',newS)
    print('After pointer:', c_wchar_p(str(newS)))

>>>(Execution terminal)
>>> from test38 import add
>>> add("test","! ")
Previous string:['t', 'e', 's', 't']
Previous pointer: c_wchar_p(140481723952464)
After string:['t', 'e', 's', 't', '!', ' ']
Pointer after: c_wchar_p(140481723970864)
>>> 

I didn't really need the ctypes module, but I learned that Python can also use C.

Recommended Posts

From a book that programmers can learn ... (Python): Pointer
From a book that programmers can learn ... (Python): About sorting
From a book that programmers can learn (Python): Decoding messages
From a book that programmers can learn (Python): Find the mode
From a book that programmers can learn ... (Python): Review of arrays
From a book that programmers can learn (Python): Statistical processing-deviation value
From a book that programmers can learn (Python): Conditional search (maximum value)
From a book that programmers can learn (Python): Class declaration (public / private, etc.)
From a book that programmers can learn ... Collect small problem parts
From a book that programmers can learn: Verification of rune checksum (fixed length)
From a book that programmers can learn ... Verification of rune checksum (variable length) Part 2
From a book that programmers can learn: Converting characters that represent numbers to integer types
From a book that makes the programmer's way of thinking interesting (Python)
8 services that even beginners can learn Python (from beginners to advanced users)
"Python Kit" that calls a Python script from Swift
I made a Docker image that can call FBX SDK Python from Node.js
Programmer's way of thinking is from XX book (Python)
"A book that understands Flask from scratch" Reading memo
Programmer's way of thinking is from XX book (Python)
A mechanism to call a Ruby method from Python that can be done in 200 lines
A memo that reads data from dashDB with Python & Spark
A Python program in "A book that gently teaches difficult programming"
python Condition extraction from a list that I often forget
A Python program that aggregates time usage from icalendar data
Call a Python function from p5.js.
[Python] Make a graph that can be moved around with Plotly
I made a package that can compare morphological analyzers with Python
Touch a Python object from Elixir
Make a Kindle book that images mathematical formulas from TeX files
Created a library for python that can easily handle morpheme division
About psd-tools, a library that can process psd files in Python
python / Make a dict from a list.
I made a shuffle that can be reset (reverted) with Python
[Python algorithm] A program that outputs Sudoku answers from a depth-first search
[python] I made a class that can write a file tree quickly
I created a template for a Python project that can be used universally
[Python] A program that finds a pair that can be divided by a specified value
Extract lines that match the conditions from a text file with python
Try using APSW, a Python library that SQLite can get serious about
[Python] I made a utility that can access dict type like a path
I made a simple timer that can be started from the terminal
Build a Python virtual environment that anyone can understand September 2016 (pyenv + virutalenv)
I made a module PyNanaco that can charge nanaco credit with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
[Python] A program that creates stairs with #
# 5 [python3] Extract characters from a character string
Create a deb file from a python package
[Python] Create a LineBot that runs regularly
Generate a class from a string in Python
Use Django from a local Python script
A typed world that begins with Python
Manipulate BigQuery tables from a Python client
A program that plays rock-paper-scissors using Python
Call a command from Python (Windows version)
Learn dynamic programming in Python (A ~ E)
[Python] A program that rounds the score
A class for PYTHON that can be operated without being aware of LDAP
I tried to create a class that can easily serialize Json in Python
I want to create a priority queue that can be updated in Python (2.7)
A python script that draws a band diagram from the VASP output file EIGENVAL
I made a familiar function that can be used in statistics with Python