Arrays are one of the important variable types when we are programming C++ apps. An array is a group of similar elements, data types, that are stored in a memory location. Array elements are defined by the data type and the variable then its size in brackets. If you are developing C++ apps with a professional C++ IDE, one of the most important parts of C programming is using data in memory efficiently. Arrays in C and C++ are one of the fastest tools in the C language. If you are asking what an array is when programming C++ or C, we explain with examples below.
Table of Contents
What is an C-style array in programming C?
In the C and C++ programming languages, in other programming languages too, an array is a collection of similar data items (int, float, char, etc) stored at a memory location in blocks. We can easily access the elements of an array by using indices of an array that has the value of that type recorded in the memory (RAM, ROM, or other media). Arrays can be used to store main data types like int, float, double, char, or some other data types such as structs, pointers, or objects in C++. Arrays are generally static arrays that have constant size. Their size in the memory is equal to the multiplication of the number of elements and the size of the element.
A C-style array is a group of similar elements, data types, that are stored in a memory location. Array elements are defined by the data type and the variable then its size in brackets. For example, if you want to define an array with char elements. Array size is declared in its definition. They are static in memory.
1 |
char a{5]; |
Array elements can be accessed using the index (position) of the element in the array. Note that array elements are always starts from zero. For example,
1 2 3 |
char a{] = "ABCDE"; printf("3rd char: %c \n", a{2]); |
In this example above, 3rd element refers to 2nd index value ( starting from 0, 1, 2) . The output of this example will be “3rd char: C”.
Here are more array examples of some data types,
What is an array in programming C++?
C-style array
C-style arrays can be used in C++ as in C examples above too. They are a group of similar elements, data types, that are stored in a memory location. Array elements are defined by the data type and the variable then its size in brackets. For example, if you want to define an array with char elements. Array size is declared in its definition. They are static in memory.
Here is the example of an array in programming C++?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> int main() { // char array char c{10]; // integer array int i[10]; // float array float f[10]; // double array double d[10]; // array of a struct struct st_p { int n; float x,y; }; struct st_p p[10]; // array of pointers of bitmaps in C++ Builder TBitmap *bmp[10]; return 0; } |
std::array (since C++11)
In C++, after the C++11 standards, array
(std::array)
is a container that encapsulates fixed size arrays. This is a C++11 array that can be used in other C++ versions like C++14, C++17, C++20, etc. You just need to include <array>
header as below.
1 |
#include <array> |
std::array
is one of the sequence containers of Containers Library which also has vector
, deque
, list
, set
, map
, etc. std::array
is an aggregate type with the same semantics as a struct holding a C style arrays as its only non-static data member. Unlike a C-style array, std::array doesn’t decay a pointer automatically. As an aggregate type, it can be initialized with aggregate-initialization given at most N
initializers that are convertible to T
ype. For example, a std::array
can be used as below,
1 2 3 4 5 6 7 |
#include <iostream> #include <array> int main() { std::array< int, 3 > a = { 10, 20, 30 }; } |
Is there an array (std::array) example in programming C++?
Here is an std::array
example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> #include <array> #include <string> #include <iterator> #include <algorithm> int main() { // ----------- an inteteger std::array ------------ std::array < int, 3 > ar = { 10, 20, 30 }; // ranged for loop can be used with std::array for(const auto& i: ar) std::cout << i << ", "; // or static_cast<int>(i) std::cout << std::endl; // -------------- a float std::array --------------- std::array < float, 5 > fl = { 9.2, 7.3, 2.4, 4.5, 5.6 }; // sort container operations can be used std::sort( fl.begin(), fl.end()); for(const auto& f: fl) std::cout << f << ", "; std::cout << std::endl; // ------------- a string std::array --------------- std::array< std::string, 4> str = { "Jim", "Ian", "David", "Yilmaz" }; for(const auto& s: str) std::cout << s << ", "; std::cout << std::endl; // copy, reverse_copy, and other container operation can be used system("pause"); return 0; } |
What are the 1D, 2D, 3D arrays in programming C++?
An array can have one or more dimensional data types. For example here are 1D, 2D, 3D array examples of a float type,
1 2 3 4 5 6 7 8 |
// 1D Array float wire{10]; // 2D Array float surface[10][10]; // 3D Array float cubic[10][10][10]; |
What is the advantage of an array in programming C++?
The C-Style array is one of the most useful memory elements in C and C++ language , you can retrieve or sort the data efficiently and relatively quickly. They are mostly preferred in faster operations in games, and some AI apps. They are very optimal in the memory usage of elements. Their size is equal to the size of the memory allocated, no more. Arrays are easy to read data from a random value and we can easily retrieve any data element located at an index position.
In C++, std::array
is modern arrays, safer in operations and it has support to container operators as same as other sequence containers like vectors etc. We highly recommend modern versions of arrays in C++ programming, but this doesn’t mean you can not use C-Style arrays.
What are the disadvantages of an array in programming C++?
Generally, C-style arrays (static ones) have a size limit, you can store only the fixed number of elements in an array. You can not resize them. But there are dynamic arrays and you can use those instead. Users may achieve unwanted memory allocations with static arrays which may cause a crash or suspension of an app. They are not safe if a developer doesn’t know or fails to check the size and extent of the array. Having an array of a fixed size with a lot of elements may cause a excessive memory usage.
In C++, std::array
is hard to remember to declare an array variable. If you are new to C++, std::array
might be more complex for your if you didn’t work with vectors and iterators etc. For example 2D, 3D types might be hard to remember how to declare them. If you have methods using std::array and you want to use them in C applications, you may need to change a lot of code lines.
What is the development tool to use an array in programming C++?
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Download RAD Studio 11 Now
See What’s New in RAD Studio 11
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition