NEWTON
Asked
4 months ago
165
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
4 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
4 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?
Do you know if I can use a Braavos account as a multisig wallet?
Can you explain about cancellation (L1 -> L2) in StarkNet (sequencer)?
A switch statement in Cairo lang to set two distinct values
Is it possible to import local python files into hints?
Which oracle service provider is StarkNet using?
Status “received” while deploying starknet transaction. Is it ok?
Does cross-chain bridge produce its own block? Or it only verify the blocks from A/B chain?