NEWTON
Asked
3 months ago
162
views
0
Hey, I want to concatenate two arrays with known sizes in Cairo lang. Is there any simple way how to do it? Can you share some of your code?
Newton
asked
3 months ago
1
Accepted answer
Hey, here is an example of concatenating two arrays of known length with the memcpy function and printing the result.
// Use the output builtin.
%builtins output
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.memcpy import memcpy
func main{output_ptr: felt*}() {
alloc_locals;
let (local arr_one: felt*) = alloc();
let (local arr_two: felt*) = alloc();
assert [arr_one] = 1;
assert [arr_one + 1] = 2;
assert [arr_one + 2] = 3;
assert [arr_two] = 4;
assert [arr_two + 1] = 5;
assert [arr_two + 2] = 6;
let (local concat: felt*) = alloc();
memcpy(concat, arr_one, 3);
memcpy(concat + 3, arr_two, 3);
assert [output_ptr] = [concat];
assert [output_ptr + 1] = [concat + 1];
assert [output_ptr + 2] = [concat + 2];
assert [output_ptr + 3] = [concat + 3];
assert [output_ptr + 4] = [concat + 4];
assert [output_ptr + 5] = [concat + 5];
let output_ptr = output_ptr + 6;
return ();
}
In case you want to concat an array of felts and an array of Points, you just have to remember that structure in Cairo is syntaxing sugar. The compiler will compute the offset of each member of the structure and allow you to access the value in memory (see here).
You can thus do the below if you want to concat your two arrays:
// Use the output builtin.
%builtins output
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.memcpy import memcpy
struct Point {
x: felt,
y: felt,
}
func main{output_ptr: felt*}() {
alloc_locals;
let (local arr_one: felt*) = alloc();
let (local arr_two: Point*) = alloc();
assert [arr_one] = 1;
assert [arr_one + 1] = 2;
assert [arr_one + 2] = 3;
assert [arr_two] = Point(3, 4);
assert [arr_two + 2] = Point(5, 6);
assert [arr_two + 4] = Point(7, 8);
let (local concat: felt*) = alloc();
memcpy(concat, arr_one, 3);
memcpy(concat + 3, arr_two, 6);
assert [output_ptr] = [concat];
assert [output_ptr + 1] = [concat + 1];
assert [output_ptr + 2] = [concat + 2];
assert [output_ptr + 3] = [concat + 3];
assert [output_ptr + 4] = [concat + 4];
assert [output_ptr + 5] = [concat + 5];
assert [output_ptr + 6] = [concat + 6];
assert [output_ptr + 7] = [concat + 7];
assert [output_ptr + 8] = [concat + 8];
let output_ptr = output_ptr + 9;
return ();
}
answered
3 months ago
Is there any other way to do 2**n without using pow.cairo?
How to submit a StarkNet contract?
How to use Argent (or any from the main providers) account in the StarkNet.py?
Is there any way to abi-encode calldata to pass to `account.execute`?
Is there any issue between using 4.4 and 3.x starknet.js?
Equivalent to Solidity's keccak256(abi.encodePacked())?
Is there a way to accelerate pytest for cairo contract?
Does Account Abstraction support "Burner Accounts"?
Does anyone know how to print a uint256 in protostar tests?
How can I prepare my Cairo lang project for an audit?
How do I connect my DAPP to Starknet mainnet using starknetjs?
Would anyone have a link or know how fast blocks would be generated on StarkNet after Regenesis ?
How to divide felt in Cairo Lang using unsigned_div_rem?
Cairo: How do write a log function in Cairo