crystal-lang


Crystal C bindings: return type char*


I have a function with this signature:
const char* get_version();
My declaration is:
fun get_version(): LibC::Char*
And to use it:
version = MyLib.get_version()
puts version # how to convert to String?
How can I manage the return string? Do I have to import also strlen to measure the length of the C string and covert it to Crystal string manually?
You wrap the Char* pointer with String.new(MyLib.version). If you know the string length, you may call String.new(ptr, size) too.
Note that this will copy the data from the pointer, so the C binding may release its pointer without affecting the Crystal String.

Related Links

How to use union in “if” statement [Crystal]
How to convert a hex string to bytes array in Crystal?
is INSTANCE = new in class is run after compilation automatically or the first time it is accessed?
Crystal lang: understanding method return type of Void
Crystal-lang httpget basic_auth
How to control using CPU cores [Crystal]
How can I read a slice with other size in crystal?
Unix-style password readline
Cannot find -lxml2 when trying to run crystal spec
How do I access an object's eigenclass in Crystal?
Why does my code never reaches the end when waiting for pipe?
Crystal reading x bytes from file
Getting the first element of an empty Array
Errors when running Crystal Play
Shorthand block syntax in Crystal
is there a way to auto recompile with crystal (aka nodemon / supervisor )

Categories

HOME
go
delphi-7
prism
c99
redux-form
jax-rs
mysql-workbench
sitecore
devise
r-googlesheets
text-mining
emoji
tput
automatic-ref-counting
plink
job-scheduling
digital-signature
nodemcu
ios10.2
jquery-callback
gmock
ckfinder
dapper-extensions
asymptotic-complexity
angular-translate
browser-sync
jaws
autodesk-designautomation
volatile
laravel-eloquent
sammy.js
carriage-return
getlasterror
python-pptx
openerp-7
diacritics
unordered-map
branching-and-merging
signal-strength
lampp
facebook-pixel
dbcontext
x-ray
singleinstance
pair-programming
custom-font
tinkerpop
java-6
git-flow
excel-dna
d3-force-directed
juice-ui
mapxtreme
omxplayer
uiactionsheet
highland.js
inf
vhosts
dynatree
lasso
ocra
keyup
java-security
gamekit
beatsmusic
cbind
jama
template-deduction
ora-00904
rfc5322
spring-3
visual-studio-express
gd-graph
jammer
windows-phone-8-sdk
script#
unit-of-work
scmmanager
facebook-timeline
font-awesome-4.0.0
insert-id
nokia-s40
fragmenttransaction
faye
listitem
cross-database
jquery-address
htdocs
google-floodlight
listactivity
dot-emacs
django-piston
lalr
lobo-cobra
google-wave
fail-fast-fail-early
perl5.12

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App