Here's my C code:
// helloworld.c
#include <stdio.h>
#include <emscripten.h>
int* EMSCRIPTEN_KEEPALIVE getIntArray(){
static int numbers[] = {1, 2, 4, 8, 16};
return numbers;
}
Here's some of my JS:
// helloworld.html
let result = Module.ccall('getIntArray', // name of C function
'Uint8Array', // return type
[null], // argument types
[null]); // arguments
let array = new Uint8Array(result,5);
console.log(result); // prints 1024
console.log(array); // prints Uint8Array(1024) [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, … ]
All of this compiles and runs fine. The above code works fine for primitive values, but fails with the array pointer, and the JS typed array I get back is all zeros. I have seen some other solutions in the documentation, but they don't seem to work for me either.
int8_t* EMSCRIPTEN_KEEPALIVE getIntArray(){ static int8_t numbers[] = {1, 2, 4, 8, 16}; return &numbers[0]; }&numbers[0]is equivalent to&*(numbers + 0)) But the point is, your function returns an address, not an array.