> For the complete documentation index, see [llms.txt](https://cs4998.cornellblockchain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs4998.cornellblockchain.org/introduction/hello-world-pt.-2/dynamic-arrays-and-strings/dynamic-arrays.md).

# Dynamic Arrays

Recall in Data Structures that we can declare a static array as follows:

```solidity
T[] arrayName = new T[](n);
```

where `T` is the type of the array. The good news is that defining a dynamic array is not that much different; below is the syntax for declaring a dynamic array:

```
T[] arrayName;
```

Declaring a dynamic array seems simple, but what about defining a dynamic array? Below is the syntax to define a dynamic array:

```solidity
T[] arrayName = [] // Insert array elements here
```

Since *dynamic arrays* can vary in size, we would ideally like to have functions available to us which allow us to grow/shrink our array. And indeed, Solidity provides us with such functions.&#x20;

* `array.push(x)`: pushes `x` to the back of `array`
* `array.pop()`: pops the last element of `array`

And like with static arrays, we can also index into dynamic arrays; it should be noted, however, that this can cause runtime errors if the user is attempting to index into an undefined location of a dynamic array.

{% hint style="info" %}

#### Dynamic Arrays in Memory?

As with mappings, dynamic arrays are reserved only for state variables; one cannot declare/define a new dynamic array variable within a function.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cs4998.cornellblockchain.org/introduction/hello-world-pt.-2/dynamic-arrays-and-strings/dynamic-arrays.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
