Arrays in C ++

Hi friends, The subject of this article is Arrays. I’m going to try to explain what we need to know about arrays.

Introduction to Arrays

Array can be called the cluster where the same type of data is collected. int, float, double, char, such as array can do.

Declaring Arrays

int score [5]; // We have defined an array of type int with a size of 5. And we called our score score.

score [0], score [1], score [2], score [3], score [4] // elements of our series

Since the index starts from 0, one of the dimensions is missing.

Accessing Arrays

cout << score [3]; // this way, we can reach the index 3 of the array and write to the screen.

score [n + 1] = 67; // we can assign any value we want to any index of the directory.

// sample code snippet3

We can do many operations with arrays. Taking the value from the user, filling in the bi directory, finding the smallest, largest element of the array, writing all the elements of the array to the screen etc. many operations can be performed.

Defined Constant as Array Size

It is always more advantageous to define the size of an array as constant.

Increases readability. (readability)
increases productivity. (Versatility)
provides sustainability. (maintainability)
// sample identification
const int NUMBER_OF_STUDENTS = 50;

Ranged-Based For Loop

With C + + 11 version, it has become easier to do loops for a given range of given array.

Initializing Arrays

When defining the Array we have specified that it should have a type, name and size. When initializing, using {}, we assign the first value to the indexes.

Example use: int children [3] = {2, 12, 1};
// the above operation gives us a value of 2, 2, 1, 2, 1, 2, 3, 1, 3, 3
children [0] = 2;
children [1] = 12;
children [2] = 1;

Auto-Initializing Arrays

int b [] = {5, 12, 11}; // where we initialize with 3 values, the size of the array is automatically 3.

Arrays in Functions (Using Arrays in Functions)

  • The arrays can be sent as a parameter to the functions.
  • Functions can return an array.
  • The functions can use any index of the array as a parameter. -> myFunction (myArray [3]);
  • Array type and size can be sent to the function with different parameters. -> void fillUp (int x [], int size);
  • The size of the array is not required where the function is called, without brackets, it is called by its name only. -> fillUp (score, sizeOfScore);
  • If we don’t want to make a change to the array we send as a parameter, we can send it as a constant parameter. Thus, we guarantee that no changes will be made to the array. -> double average (const int a [], int size);
  • Series; We can easily use it when programming for sorting, searching.

Multidimensional Arrays

char book [24] [120]; // two-dimensional array

book [0] [0], book [0] [1] [ [0] [119]


book [23] [0], ………………………………………………… book [23] [119]

Finally, it should be noted that arrays of arrays start at 0. If the size of the array is exceeded, the program jumps to a point that we do not want and can receive a segmentation fault. Although it does not cause a visible error, it can also lead to incorrect operations.


if you have question do not forget to write from the chat button next to it or from the comment

You may also like...

1 Response

  1. foloren torium says:

    I like this web site its a master peace ! Glad I observed this on google .

Leave a Reply

Your email address will not be published. Required fields are marked *