-
-
Save vydd/6484a05906ebc10595f6b7e15b41d048 to your computer and use it in GitHub Desktop.
CFFI calling char* with a vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ql:quickload :cffi) | |
(cffi:define-foreign-library vowels (t (:default "/path/to/libvowels"))) | |
(cffi:use-foreign-library vowels) | |
(defvar *vector* #(104 101 108 108 111)) | |
(print (cffi:foreign-funcall "vowels" :string (map 'string #'code-char *vector*) :int)) | |
# prints out 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcc vowels.c -shared -o libvowels.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
int vowels(char *str) { | |
int count = 0; | |
for (int i = 0; i < strlen(str); ++i) { | |
switch (str[i]) { | |
case 'a': | |
case 'e': | |
case 'i': | |
case 'o': | |
case 'u': | |
count++; | |
break; | |
default: | |
break; | |
} | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment