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,

close

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.

Related posts
C++Learn C++Videos

Learn C++ With Monoids, Monads, and Applicative Functors: Repeated Software Patterns (CPPCon 2020) Video

C++Learn C++Videos

Learn C++ With Pipes: How Plumbing Can Make Your C++ More Expressive by Jonathan Boccara (CPPCon 2020) Video

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

Learn Classes and Objects in C++

C++C++11C++14C++17Introduction to C++Learn C++

Learn about Access Specifiers in C++ Classes

Worth reading...
Learn To Use For Loops In C++
en_USEnglish