How can I use memcpy in a Vector in CAPL to initialize an array with zeros? The variable in CAPL is:
byte myArray;
In C the solution to init the array would be:
memset(myarray, 0, sizeof(myarray));
How can I do the same in CAPL?
CAPL do not support dynamic allocated arrays/memory and pointers, so you are forced to use statically allocated arrays. You need to know in advance the array length.
To declare array we should use :
Static Allocation
byte myArray[10];
or Automatic allocation
const int size = 10;
byte myArray[size];
We can also declare array and initialize it:
byte myArray[10]={0,0,0,0,0,0,0,0,0,0};
To initialize array with some values use for loop:
for (i = 0; i < size ; i++)
{
myArray[i] = 0;
}
memset()
Converts the value to unsigned char and copies it into the block of memory. CAPL does not support memset()
instead you can use memcpy()
(copies from one place to another) in CAPL.
on sysvar sysvar::x::y
{
byte data[4];
byte data1[4];
struct WrapDword
{
dword dw;
} dwordWrapper;
int i;
// initialize byte array with some values
for (i = 0; i < elcount(data); ++i)
data[i] = i;
// Copy bytes from array to a destination type dword.
memcpy(dwordWrapper, data);
write("Bytes as dword: %0#10lx", dwordWrapper.dw); // 0x03020100
// Copy dword as bytes into data1
memcpy(data1, dwordWrapper);
write("dword as bytes: %#lx %#lx %#lx %#lx", data1[0], data1[1], data1[2], data1[3]); // 0 0x1 0x2 0x3
}