7.2 Declaring Arrays

Now that we have an idea of what arrays are, let’s take a look at how to declare, or create, one. In order to declare an array, use the following syntax:
int nums[10];
There are some different components in the declaration of an array. The first component represents the type of data you want to store in the array. Since we used int to declare our array, we will be able to store integers within this structure. Next, we gave our array the name nums. You can name your array anything you want, but a good rule of thumb is to name the array something that has to do with the data you are storing in it. For example, if I wanted to create an array that would store the different ages of kids in a class, I would call the array ages or something similar. After naming the array, you should insert a pair of brackets, inside of which you should type the number of elements you want your array to contain. In nums, I want to store 10 integers, which is why I set the size of the array to 10. Once you make an array a certain size, you will not be able to change the size of the array at any later point in your program. Finally, as always, place a semicolon after the declaration to signal the end of this line of code.

Previous Section

Next Section

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.