Hi I imported a c function who take a char array as argument. I pass a Nim string to it but the 3 first characters aren't expected, like this one "|" and random special chars who can't be displayed by the forum.
Code example
import streams
#import the c function.
proc print_string*(input_string:string, size:int) {.header: "cfunction.h", importc:"print_string".}
#get the text.
var testString:string = readAll(newFileStream("testfile"))
#call the function imported from c file.
print_string(testString, testString.len())
And the c function, it's a dumb example who displays the problem.
void print_string(const char input_string[], int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%c", input_string[i]);
}
}
Sorry for my ignorance and thanks in advance for your help.
Correct:
proc print_string*(input_string: cstring, size: cint) {.header: "cfunction.h", importc:"print_string".}
Thanks again cdome.
I have another question with C but I want to avoid polluting the homepage with noobish topics. And strings are kind of sequence isn't it ;) ?
I want to pass a sequence to a C function but the result is unexpected.
Two files for minimal example.
proc print_array(tabl:seq, size:cint) {.header:"function.h" , importc.}
var test = @[
-0.5f64, -0.5, 0.0,
0.0, 65.2, 0.0,
0.5, -0.5, 2564.5684
]
print_array(test, cint(test.len()))
// no header guard for conciseness.
#include <stdio.h>
void print_array(float arrTest[], int size)
{
int i;
for (i= 0; i < size; i++)
printf("test[%i]: %f \n", i, arrTest[i]);
}
output
test[0]: 0.000000
test[1]: 0.000000
test[2]: 0.000000
test[3]: 0.000000
test[4]: 0.000000
test[5]: -1.750000
test[6]: 0.000000
test[7]: -1.750000
test[8]: 0.000000
I trying many stupid things whithout success. Surely a lack of understanding about what I'm doing.