RosettaCodeData/Task/Call-a-foreign-language-function/Crystal/call-a-foreign-language-function.crystal
2023-07-01 13:44:08 -04:00

12 lines
355 B
Text

@[Link("c")] # name of library that is passed to linker. Not needed as libc is linked by stdlib.
lib LibC
fun free(ptr : Void*) : Void
fun strdup(ptr : Char*) : Char*
end
s1 = "Hello World!"
p = LibC.strdup(s1) # returns Char* allocated by LibC
s2 = String.new(p)
LibC.free p # pointer can be freed as String.new(Char*) makes a copy of data
puts s2