for example
unsigned char imgdata[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0...};
Ihandle* image = IupImageRGBA(32, 32, imgdata);
I tried
var imgdata = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0...}
var image = iupImageRGBA(32, 32, imgdata)
but obviuolsy it is wrong. So what is the proper way to make and pass a C array to C function? thanksIn C and C++, when you use an C array as parameter of a function, then the compiler automatically passes a pointer to the first element.
In Nim, we may define our functions in a way that arrays and seqs are of type open array, so we can pass them directly. But if your function expects a pointer, then you may have to use addr operator and maybe additional a cast. Maybe something like
var imgdata = [0, 0] # array, use @[0, 0] for a seq
var image = iupImageRGBA(32, 32, cast [MyDesiredType](addr(imgdata[0]))) # should work for array and seq
var image = iupImageRGBA(32, 32, cast [MyDesiredType](addr(imgdata))) # works for array only
I'm a bit careful and general in my reply because I don't know the library you use. First, in Nim an array has a known and fixed size. If you need a dynamic array have a lot of seq.
Basically you should differentiate between two cases:
Examples for both would be
// type 1, with size
int foo1(char *bar, int bsize); // 'bsize' is the number of chars in 'bar'
// type 2, no size
int foo2(char *bar);
The proper declaration and calling of those funcs in/from Nim:
proc foo1(arr: openarray[char]): cint {.importc.}
proc foo2(arr: ptr char): cint {.importc.}
#...
var myArr: array[42, char]
let res1 = foo1(myArr)
let res2 = foo2(addr(myArr[0]))
Note that Nim automagically passes both, a ptr to the array data and the size of the array to a C func declared to have an openarray parameter.
In your case - and assuming imgdata has a fixed size, e.g. 10 - something like this
type
Image = object
# fields
Ihandle = ptr Image
proc iupImageRGBA(p1, p2: cint; imgArr: ptr byte): Ihandle {.importc.}
var imgdata = [0'byte, 0, 0, 0, 0, 0, 0, 0, 0, 0] # fixed size! array
var image: Ihandle = iupImageRGBA(32, 32, addr(imgdata[0]))
should do what you want.
Warning: my example is based on certain assumptions (e.g. the first 2 parameters being of type cint). I strongly suggest to make the effort of providing all needed info when asking questions to avoid misunderstandings.