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.

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Language FeatureLearn C++

What Are Generalized Constant Expressions (constexpr) In C++?

C++C++11C++14C++17Learn C++

What Is The alignas Alignment Specifier In Modern C++?

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

What is a Forward Declaration enum Enumeration in C++?

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

What Is Static Assertion And How To Use static_assert In C++?

Worth reading...
Discover Switch Statements in C++