Hello, I'm trying to bind to C code, and I have various questions around C
strings (cstring)
As I understand, cstring is suitable for both const strings in C (`const
char*`) as well as mutable strings (`char *`, as long as the length is
respected). Now, I need to pass a string buffer to a C function so it can be
filled (like would do snprintf) and I don't know how I should allocate the
cstring from Nim.
let s: string = newString(1024)
let cs: cstring = cstring(s)
snprintf(s, 1024, "Foo bar %s", foobar)
echo s
Run
Would that actually work? it does not feel good to allocate a string to convert
it to a cstring. What's the best way to do here?
I also have another question to write C structs in Nim. Sometimes, in a struct,
a string is embedded (without a pointer indirection) as an array of chars.
Something like this:
#define MAX_LENGTH 1024
struct foo {
char name[MAX_LENGTH]
}
Run
I translated it in Nim as:
const MAX_LENGTH = 1024
type foo = tuple
name: array[MAX_LENGTH, char]
Run
Is that correct, is the memory layout the same? And then, how do I convert the
array to a cstring to use in my code?
Thank you.