ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

C++ Chapter 9: C++ all about Arrays

prodevelopertutorial January 26, 2020

In this chapter we shall study about following topics:

  1. One Dimensional array
  2. Characteristics of an array
  3. Accessing array elements through pointers
  4. Arrays of pointers
  5. Passing a single array element to a function
  6. Passing whole array to a function
  7. Two dimensional array

 

An array is a collection of elements of same type. It is a linear and homogeneous data structure. Once the array is created, its size if fixed. You cannot increase or decrease the length of an array.

 

1. One Dimensional array

Syntax for 1D array:

<data-type> <variable-name> [ size ];

 

Example:

    int arr [20];

Initialization of an array:

    int arr[5] = {1, 2, 3, 4, 5};

 

 

2. Characteristics of an array

  1. Creating an array of 5 integers is equal to creating 5 integer variable.
  2. Array elements are accessed through index.
  3. Index for an array starts from 0.
  4. Total size of the array is equal to size of data-type * number of elements in the array.

Simple example to store the elements in an array and display them.

 

#include<iostream>
using namespace std;

int main()
{

    int arr[5];
    cout<<"enter 5 numbers"<<endl;

    for (int i = 0; i < 5; ++i) // as we are starting the array from 0.
    {
        cin>>arr[i];
    }
   
    cout<<"The entered elements are"<<endl;

    // here the elements are accessed one by one.

    cout<<arr[0]<<endl;
 
    cout<<arr[1]<<endl;

    cout<<arr[2]<<endl;

    cout<<arr[3]<<endl;

    cout<<arr[4]<<endl;

}

Output:

 

enter 5 numbers

5

4

3

2

1

The entered elements are

5

4

3

2

1

3. Accessing array elements through pointers

As arrays are stored in contiguous memory locations, the array elements can be accessed through pointer.

Below is the example for the same:

 

#include<iostream>
using namespace std;

int main()
{

    int arr[5] = {1, 2, 3, 4, 5};
    int *ptr = NULL;

    ptr = &arr[0]; // assign the address of first variable of the array to the pointer

    for (int i = 0; i < 5; ++i)
   {

      cout <<" The address of arr["<<i<<"] is = "<< (ptr + i)<<" The value is = "<< *(ptr + i)<<endl;
   }

}

Output:

The address of arr[0] is = 0x7ffeebf53ad0 The value is = 1

The address of arr[1] is = 0x7ffeebf53ad4 The value is = 2

The address of arr[2] is = 0x7ffeebf53ad8 The value is = 3

The address of arr[3] is = 0x7ffeebf53adc The value is = 4

The address of arr[4] is = 0x7ffeebf53ae0 The value is = 5


4. Arrays of pointers

Arrays can also store the address of the variables. Then they are called as arrays of pointers.

 

Example:

#include<iostream>
using namespace std;

int main()
{

    int *arr[3];

    int num_1 = 10;

    int num_2 = 20;

    int num_3 = 30;


    arr[0] = &num_1;

    arr[1] = &num_2;

    arr[2] = &num_3;


    for (int i = 0; i < 3; ++i)
    {
        cout <<" The address of number ["<<i<<"] is = "<< arr[i] <<" The value is = "<< *arr[i] <<endl;
    }
}


Output:

 

The address of number [0] is = 0x7ffee0782ab8 The value is = 10

The address of number [1] is = 0x7ffee0782ab4 The value is = 20

The address of number [2] is = 0x7ffee0782ab0 The value is = 30

5. Passing a single array element to a function

A  single element can be sent to a function with the help of the index of that variable.

 

Example:

#include<iostream>
using namespace std;

    void display(int a);

int main()
{

    int arr[3] = {1, 2, 3};

    for (int i = 0; i < 3; ++i)
    {

        display(arr[i]);
    }

}

void display(int a)
{
    cout<<a << endl;
}


Output

1

2

3


6. Passing whole array to a function

A whole array can be passed to a function, by sending the address of first element.

Example:

#include<iostream>
using namespace std;

void display(int arr[] , int length);

int main()
{

    int arr[3] = {1, 2, 3};
    
    display(&arr[0], 3);

return 0;

}

void display(int arr[] , int length)
{

    for (int i = 0; i < length; ++i)
    {
        cout<<arr[i] << endl;
    }

return;

}

Output:

1

2

3


7. Two dimensional array

The visualisation of 3 * 2, 2D array is shown in below image.

2d array

 

Below is the program to show 2D array:

#include<iostream>
using namespace std;

int main()
{
    int arr[3] [2] = {1, 2, 3, 4, 5, 6};

    for (int row = 0; row < 3; row++)
    {
        for (int column = 0; column < 2; column++)
        {
            cout<< " "<< arr[row] [column];
        }

        cout<<endl;
    }

}

Output:

1 2

3 4

5 6


List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT