Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
How to pass an array into a function without using vectors in emscription?

I've looked basically everywhere, and I can't find any documentation about this question. I want to pass a JS array as a C-array in em++, and everything I've found uses vectors. With vectors, you have to push back every value one by one then pass it into the C++ function. This is slow and really inconvenient, so I'd like to know of a normal C-array way of doing this.
For context, I want to do something like this:

int
add(const int test[], const int size)
{
    int res = 0;
    for (int i = 0; i < size; i++)
        res += test[i];
    return res;
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do it with this syntax (using int** will lose size information, What is array to pointer decay?):

#include <utility>

// For an array of size 4
int add4(const int(&values)[4])
{
    int sum{ 0 };

    // if you cant use range based fors
    for (std::size_t n = 0; n < 4; ++n) sum += values[n];
    return sum;
}

// For any const sized array
// with this syntax you don't lose size information on the array
template<std::size_t N>
int add(const int(&values)[N])
{
    int sum{ 0 };

    // I prefer range based fors
    for (const int value : values) sum += value;
    return sum;
}


int main()
{
    int values[4] { 1,2,3,4 };
    int sum4 = add4(values);

    // compiler will know array has size 8
    int values8[]{ 1,2,3,4,5,6,7,8 }; 
    int sum8 = add(values8);

}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!