logos-storage-docs-obsidian/10 Notes/How to get a pointer to a seq to pass it to a C library.md
2025-07-02 19:55:28 +02:00

22 lines
761 B
Markdown

Having a Nim sequence:
```nim
var buff = newSeq[byte](buffSize)
```
if you want to get a pointer to the underlying data buffer that is suitable for being passed to a C library, you can simply use:
```nim
sha256Update(ctx, addr buff[0], input[i].len.uint)
```
If your input is a `string`, you can use the following:
```nim
let str = "abcdefghijklmnopqrstuvwxyz"
sha256Update(ctx, str.cstring, input[i].len.uint)
```
Here we use `sha256Update` from [[BearSSL]] library. See [[How to create a hash using BearSSL]] for more complete examples.
See also [Accessing seq pointer](https://forum.nim-lang.org/t/1489), [Use cstring for C binding](https://forum.nim-lang.org/t/8179), and [Arrays and Sequences in nim](https://forum.nim-lang.org/t/5703) on Nim forum.