Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda