C++ One-Dimensional Arrays

One dimensional array are the simplest form of an array in C++ language. You can easily declare, initialize, and manipulate a one-dimensional array. A one-dimensional array can be a parameter for function and so on. You can treat individual array element like any other C++ variables.

Advertisements

1-D Array Declaration

The syntax to declare a one-dimensional array is very simple. Here it is,

<data_type>  <array_name> [n];

The two square brackets hold the number of elements in the array denoted by n.

Example#1

float demo[10];

The above array can hold 11 elements because the index associated with each array element starts with 0. The first element has an index value of 0.

1-D Array Initialization

There are different ways to initialize an array. You must provide values to each array element before using them in your program.

The syntax to initialize a one-dimensional array is as follows.

<data_type> <array_name> [n]; //array declaration first 
<array_name> [n] = {<array_value1, array_value2, ..., array_value n-1}; //value assignment
or
<data_type> <array_name> [n] = {<arrayvalue ,   array_value2 , ... , array_value n-1};

You can use any suitable method given. Here is an example for one-dimensional array declaration.

Example #2

int toyota_car [5]; //array declaration
toyota_car [5]= { 2345, 4566, 7766, 3456, 9898}; //array value assignment

Example #3

char superheroes[3] = {"spiderman", "superman", "ironman"};

or

you can do the declaration and assignment without specifying a number. See the example below.

Example #4

char superheroes[] = {"spiderman", "superman", "ironman"};

In the above example, we never provide any number to the subscript, the assignment of 3 values will determine the length of the one-dimensional array.

How To Pass An Array In A Function

The size of the one-dimensional array is large and it is difficult to pass an entire array as a function parameter. This will crash the memory or slow down the program. However, arrays are different than normal variables, they are like pointers, a variable that refers to another variable instead of holding value. Only some characteristics of pointers are adopted by arrays.

Advertisements

The first element of the array has an index value of 0. If you use the name of the array, then it refers to the memory address of the first element of the array. Using the first element, you can find the memory address of rest of the elements and this is done by adding an offset to the memory address of the first element.

For example,

arrayT  has address 0x256778, then

arrayT + 1 has the address of the next location, and so on.

*(arrayT+ 1) gives the value stored at the next location. See the image below for better understanding.

Array Index and Value

Therefore, to pass an array as a function parameter, you need to pass only the first element memory address and the rest of the array can be calculated automatically.

Function Declaration With Array as Parameter

You must declare a function as usual and declare the array as a parameter for the function. Check the example below.

int calculate_sum(int arrayT[]); //function declaration

Note that we have not specified a number for the subscript. You can do it separately.

Function Definition With Array as Parameter

Function definition must contain the array with type as a parameter.

int calculate_sum(int arrayT[])
{
      Some code;
}

Function Call with Array as Argument

The function call is the most important part that contains the actual argument. Here you must pass only the first element memory address and any other values for the function.

int calculate_sum(arrayT);

The following example program demonstrates the concept.

Example Program:

/* This program demonstrate the use of memory address of the 
first element of an array and pass it as a parameter to a function.
The function use a different subscript notation (a + i) to compute sum
of all elements of the array */
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
    int arr[5]={23,45,67,112,22};
    int arrpara(int arr[]);
    arrpara(arr);
system("PAUSE");
return EXIT_SUCCESS;
}
int arrpara(int arr[])
{
    int sum = 0;
    int i;
    for(i=0;i<5;i++)
    {
        sum = sum + *(arr + i);
    }
cout << " first element =" << " " << arr << endl;
cout << "sum =" << " " << sum << endl;
} 

Output:

first element = 0x28ff20
sum = 269


Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version