C++ Refresher - Part Three

You can visit the previous parts of the article series on C++ Refresher here:

Here is the third part of the C++ Refresher.

In this session, you will learn about:

  • Arrays
  • Types of arrays
  • Structures
  • Array of structures

You have already learned the above concept of arrays and structures in C#. Now this session discusses how these concepts are implemented in C++ and what are the similarities and differences in these concepts with respect to C# and C++. Managing multiple variables is a tedious task.

Managing multiple variables

Working with Arrays

An array is a collection of elements of similar data types stored in adjacent memory locations. Before an array variable can be initialized, its data type and size (number of elements in the array) must be defined.

The syntax to declare an array is:

<data type><array_name>[<dimension/size>];

Example:

  1. int arr[5];  
The size is also known as the dimension of the array.

The following figure shows the placement of the array elements in the memory.

placement of the array

The array elements are placed adjacent to each other in the memory. The elements of an array in C++ are recognized by their position in the array known as the subscript and the index number. That subscript/index along with the array name is used to access the elements of the array. Therefore, the size specified at the time of array declaration, decides the values that an array can hold.

array declaration

In C++, you can initialize an array either at the time of declaration or after declaration.

The process of assigning values to an array is known as initialization. The method used for initialization depends on the programmer.

Let us see what are the different ways to initialize an array.

different ways to initialize an array

The size of array

The method of initialization shown in the following example is valid.

Example:
  1. int abc[3]={100,200,300}, xyz[3];  
  2. xyz[2]=abc[1];  
  3. xyz[0]=abc[2];  
  4. xyz[1]=abc[0];  
Types of Arrays

C++ supports the following types of arrays:

 

  • One-dimensional
  • Multidimensional

Different types of arrays supported by C++:

Different types of arrays

One-Dimensional Arrays

arrays

Example:

  1. char str[5];  
In C++ , a character array is used to store and manipulate a string. A string is a collection of characters that ends with a special character known as NULL('\0').

In C++, character arrays can be initialized in different ways. Let us see what the different ways to initialize character arrays are.

Note that the last element has been initialized with the ‘\0’ (single quotes) character explicitly.

Example

When an array is initialized with a string, the ‘\0’ character is added automatically at the end of the string. An array can be initialized with a complete string with the help of double quotes (“”).

Example:
  1. char str[5]=“JOHN”;  
Therefore, the size of the array, str will be 4+1=5 i.e four characters and one NULL character. The size of the array includes the total number of characters including the NULL character.

Example:
  1. char str[5]=“JOHN”;  
The string stored in a character array can be displayed using the cout object.

Example:
  1. char str[5]=“JOHN”;  
  2. cout<<str;  
  3. cout<<str[0]  
Multidimensional Arrays

Multidimensional Arrays

In situations where more than one string needs to be stored in an array, a collection of rows and columns is required, similar to a matrix.

Declaring and Initializing Multidimensional Arrays

The syntax to declare a multidimensional array is:

<data type><array_name>[<dimension1/rows>] [<dimension2/columns>];


Example:
  1. char names_of_two_students[2][4];  
The total number of elements in the array is 2*4=8

total number of elements

The following figure shows the representation of a multidimensional array in the memory.

multidimensional array

Each row can store one string of four characters including the NULL character. The elements of the array are recognized by their position in the array known as the subscript or the index. To display the value stored in the first row, you need to specify the row index number with the array name. The index of first row is 0 and the index of the second row is 1. Similarly the array contains four columns with the indices 0,1,2,3 respectively. A particular character in the array, you require row as well as column index i.e. 0, 0 or 0, 1.

Working with Structures

Arrays allow you to group values of the same data type under one name. However, arrays cannot store values having disparate data types. If you want to store values having disparate data types, you need to use structures.

Defining Structures

Structures are user defined data types that are used to store disparate data, which is logically related. A structure can hold values of different data types like char, int, or float.

A structure is defined by using the struct keyword.

struct keyword

Another way to declare a structure variable is after the declaration of a structure. The declaration of a structure does not allocate memory for the structure. To allocate memory for the structure, you need to declare a structure variable.

structure variable

A structure does not allocate any memory for the structure. After creating a structure variable, you can access the member variables of the structure by using the dot (.) operator.

Example:
  1. salesrec.salesno;  
  2. salesrec.transno[1];  
Array of Structures

Structures of Array

Array of Structures

Note that the structure array, stud_records has been initialized with four records at the time of declaration. Therefore, dimension of the array has been omitted.

dimension of the array

Differentiate between Class and structures.

Class Structure
It is a reference type. It is a value type.
It can be inherited. It cannot be inherited.
Class members are private by default.Structure members are public by default.

Read more articles on C++:


Similar Articles