C++Introduction to C++Learn C++

Learn To Define And Use Arrays In C++

Arrays are used to define a data block in the memory with number of data types, like integer numbers, floating point numbers, characters, structures, … etc. C & C++ programming language provides this data structure, called as array that stores a fixed-size of elements of the same type. Arrays are generally used to store a collection of data, and it is often more useful to store data as a collection of variables of the same type. We can also store multiple data types of variables in arrays by using structure (struct) elements.

Arrays are very good to use variables with their index numbers, instead of using each for each variables. For example we can use 6th string name element as name[5] instead of using name0, name1, name2, name3, name4, name5. All arrays consist of contiguous memory locations, their index number starts by 0, so we can not use negative numbers as a index, and we can not exceed maximum number of elements. The lowest memory address corresponds to the first element index number and the highest address to the last element index number.

Basically, we can illustrate this in general form as below,

Here are some array examples,

1. Initializing Arrays

We can initialize array elements in C && C++ programming language as below,

We can init arrays without number of elements as below,

ASCII strings are also char arrays, we can define a ASCII string as below

2. Accessing to Elements of Arrays

We can access any array of element by using it’s index number.

We can access arrays by using for() , while() or do-while() loops, so this example can be written easily as below,

We can access to each character of char arrays as below,

3. Initializing and Accessing Multi-Dimensional Arrays (2D, 3D … arrays)

All array examples given above were one dimensional arrays. We can initialize and access to 2D, 3D or multi dimensional arrays, they are also called as matrix forms. While we live in 3D environment, in mathematics we can define and do calculations more than 3 dimensions.

Here is an example to init and access to a 2D matrix form with integer numbers;

Here is an example to init and access to a 3D matrix form with integer numbers;

Generally we read and write these values by using for loops. For example for a two dimensional array we should use two loops for i & j indexes. See how we can set variables and how we can print out them as below,

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?

C++C++14C++17C++20Learn C++

What Are The C++14 Features Removed From C++17?

C++C++11C++14C++17C++20Learn C++Syntax

What is the conjunction (std::conjunction) metafunction in C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is The disjunction (std::disjunction) Metafunction In C++?

Worth reading...
Discover If Statements and Conditions in C++