Dynamic arrays

Dynamic arrays refer to arrays that do not have a pre-determined size at the time of declaration; however, their size is determined at runtime. Take a look at the following code:

int[] age ; // array of int with no fixed storage space allocated. Storage is allocated during assignmentbyte[] flags ; // array of byte with no fixed storage space allocated

Dynamic arrays can be initialized inline or using the new operator. The initialization can happen at the time of declaration as  follows:

int[] age = [int(10), 20,30,40,50] ;int[] age = new int[](5) ;

The initialization can also happen within a function later in the following two different steps:

int[] age ;age = new int[](5) ;

Get Solidity Programming Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.