How to cast an array to an string array?

Open
#179 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
25/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Stale
Tech stack
javascript, node.js
Domain
backend

Research direction

Start by comparing wrapper.cc and index.js with the linked array_buffer_to_native example, focusing on how the N-API array is passed into native code. Check whether the repository contains another string-array example. Done means providing or documenting a working example for the shown char** use case.

Written by the indexing model from the issue text.

Description

I'm trying to wrap a C function that receives a char** as parameter but I can't seem to cast it properly. Do I have to iterate over the array and recreate an array on c? I was following this array buffer to native example but I'm stuck. Can someone give me a hint or point me to an example that uses strings and array?

// index.js
const addon = require('./node/addon')

addon.start(4, [
  '-in',
  'str1',
  '-out',
  'str3'
])
// wrapper.cc

Napi::Value start(const Napi::CallbackInfo &info)
{
    Napi::Env env = info.Env();

    Napi::Number bufferSize = info[0].As<Napi::Number>();
    Napi::Array buffer = info[1].As<Napi::Array>();

    // how would you make this work?
    char **stringArray= reinterpret_cast<char **>(buffer);
Dominant language
C++
Stars
2.6k
Forks
602
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nodejs/node-addon-examples

All issues in nodejs/node-addon-examples

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.