Solidity-013 MemoryToMemoryReferenceTypeAssignment

pragma solidity ^0.8.9;

contract MemoryToMemoryReferenceTypeAssignment {

    // Function 'getUInt' returns an unsigned integer

    function getUInt() public returns (uint)

    {

        // Declare a new dynamic array 'someVar' in memory with 1 element

        uint[] memory someVar = new uint[](1);

        // Set the first element of 'someVar' to 23

        someVar[0] = 23;

        // Declare another memory array 'otherVar' and initialize it with 'someVar'

        // This creates a reference to the same memory location as 'someVar'

        uint[] memory otherVar = someVar;

        // Modify the first element of 'someVar' to 45

        // Since 'otherVar' references the same memory location, it is also updated

        someVar[0] = 45;

        // Return the first element of 'otherVar'

        // It returns 45, demonstrating that both arrays reference the same memory

        return (otherVar[0]);

    }

}

//Dploy screenshoot show:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值