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.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
CC++C++17Introduction to C++Learn C++

How To Run A C++ Program In The Linux Terminal

C++C++17Game DevelopmentLearn C++

How To Program A Game In C++

C++C++17Introduction to C++Language FeatureLearn C++

What Is C++ Programming Used For?

C++C++17Introduction to C++Language FeatureLearn C++

How To Run A C++ Program

Worth reading...
Discover Goto and Labels in C++