Arrays in Solidity: Fixed Size Arrays
Base·Mon, Oct 9, 2023, 12:00 AM
Enhance your Solidity skills with fixed-size storage arrays. 📚🚀 Discover how to initialize and modify elements in these arrays, and grasp the distinctions between dynamic and fixed-size arrays. Learn how to efficiently access and manipulate data for improved smart contract development. 💡💻 📚 Head to Base Camp for full courses on smart contract development: https://docs.base.org/base-camp/docs/welcome ✅ Subscribe to our channel here: https://www.youtube.com/@BuildonBase ► Build on Base: https://base.org ► Follow us on Twitter: https://twitter.com/buildonbase ► Join our Discord: https://base.org/discord
Transcript
In this lesson, we will learn more about fixed-size storage arrays. We will continue the previous example. But let's start making some changes. If you remember, we declared a fixed-size array with 10 elements. In Solidity, we can initialize arrays.
Say with 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. this way. Now we initialize a fixed array with 10 elements. And actually we can do the same thing with dynamic arrays. Okay.
And now the dynamic array initially has two elements and then we can add more elements. Now, similar to what we did with the dynamic array, let's add elements to the fixed array. So, let's write a function called add element to fixed array. And this will be very similar to the previous example. We will insert let's say two uint256s named new element and external.
Now let's try to write a fixed array of push points and new elements. Okay. Now if you notice, the compiler gives an error message and this is because the push function as well as the pop function are not available for fixed sized arrays . This function is only for dynamically sized arrays. If you look at this, our array does have 10 elements.
So since we can't add elements to an array this way, what we can do is edit the elements in this array. So we can say I want to change the element at index underscore index. Then we enter the element that we want to place at that index. So instead of using dot fixed array.push, what we are saying is we are accessing the index meaning the array position at that index and we are setting its value to a new element.
Okay. So let's test it. We will deploy arrays. And now we have add element to fixed array which accepts two parameters. New indexes and elements.
Let's verify first. Let's say the zero index of the array is fixed. Has a value of one. Now let's say we want to change the zero index of a fixed array. So we enter a value of zero and then we set the value to say 20.
We do the transact and now let's verify. OK, as you can see we access the array at the position we specified and then we change its value. Now let's say we want to create a function that returns the first five elements of that array first five elements of that array . Let's create a function named get first five elements of fixed array. Okay first five elements of fixed array.
Okay . Then we can say this will be an external but since it returns the first five elements, we need to specify uint256. as many as five and I will explain more about this memory keyword. So, since we need to return the first five elements, the only way is to declare a temporary array. In this case, let's call it the result.
Okay, it should be in memory. Then what we need to do is iterate over that fixed array. We will store those five elements into the result array and then we will return the result. So here we say return the result but now we need to insert the elements. So we're going to create a for and we're going to declare a uint256 index.
So we declare an index variable where index will be less than five because we want the first five elements then we simply increment the index by one. So now we fill the result array in memory. So this is only within the scope of that function. And we say the elements that will start from zero that will start from zero will contain a fixed array at that index as well. Okay.
Then we will do iterations one, two, three, and four. So we will enter the first five elements and the last element will have index four. Okay. And that should work. Now we cannot restrict immutability to views because we are not modifying the storage but we are accessing the storage.
Okay. Now everything is green. Let's summarize everything is green. Let's summarize . To get the first five elements .
To get the first five elements , we need to declare an in-memory array which is a temporal array that is within the function scope and must be declared as an immutable array because it is a very important aspect. Solidity does not have dynamically sized arrays in memory. Okay. And the reason is because when you call a function, the function must know in advance the amount of memory that needs to be reserved for the execution of this particular function. So if solidity doesn't have it, it's impossible to know what memory needs to be allocated.
So for that reason, in-memory arrays must declare their size first. Okay. So, let's test it. I'm going to deploy it and I'm going to just say get the first five elements say get the first five elements . And as you can see, we get one, two, three, four, and five.
Okay. Something we can do is declare a variable there and we can do the same thing and that can be done
Automatic transcript — names and jargon may be misspelled.