C++ Builder Tutorials

C++ Builder: Structures and Arrays

Structures

A structure is a group of variables of different data types under a single name. The elements are known as members; they can have different types and different lengths.

Data structures are declared in C++ using the following syntax:

struct name
{
type membername1;
type membername2;
// and so on...
};

For example, you want to store information about employees, such as their name, age and salary:

struct employee
{
  string fullname;
  int age;
  float salary;
};

Once you declare the structure "employee" as above, you can define a variable Lisa as:

employee Lisa;

Now you assign data to the members of the structure variable Lisa. To access the members, insert a dot (.) before the member names:

Lisa.fullname = "Lisa Cplusplus";
Lisa.age = 30;
Lisa.salary = 30000;

Within the definition of a structure, you can declare one or more variables of that structure type, for example:

struct employee
{
  string fullname;
  int age;
  float salary;
} Lisa, Bill, Jeff;

You can pass structure variables to a function:

float SalaryTotal(employee Lisa, employee Bill, employee Jeff)
{
  return (Lisa.salary + Bill.salary + Jeff.salary);
}

Arrays

An array is a group of elements of the same type, that can be referenced individually with an index.

For example, 5 values of type "int" can be declared as an array without having to declare 5 different variables (each with its own identifier). Using an array, the five "int" values are stored in elements of the array, and all five can be accessed using the same identifier with the proper index.

Arrays have 0 as the first index, not 1.

Here's an array of 5 integers:

int nums[5];

To assign values to the individual elements:

nums[0] = 88;
nums[1] = 76;
nums[2] = 90;
nums[3] = 61;
nums[4] = 69;

Or, declare the array and initialize it, all in one go:

int nums[5] = {88, 76, 90, 61, 69};

Arrays can be passed to functions. Only the name of the array is needed in the argument.

float avg;
avg = ArrAverage(nums);
 
float ArrAverage(float arr[], int size)
{
  int i;
  float sum;
  sum = 0;
  for (i = 0; i < size; ++i) {
    sum += arr[i];
  }
  return (sum / size);
}

If we would modify elements of the array in the user-defined function, although we pass it under a different name arr[], the original array nums would be changed!

Examples of types of arrays

struct employee
{
  string fullname;
  int age;
  float salary;
}
 	
int age[5]; // array of integers
String months[12];
employee shop[3];

Multidimensional arrays

A multidimensional array is an "array of arrays". For example, a bidimensional array can be imagined as a two-dimensional table of elements, all of them of the same uniform data type. Example:

float price[10][2];

Multidimensional arrays are not limited to two indices (two dimensions). They can contain as many indices as needed.


Example: accessing elements of an array

float amount;
int pieces;
age[2] = 75;   
price[2][1] = 10.5;
amount = pieces * price[2][2]; 
shop[0].fullname = "Lisa Cplusplus";
shop[0].age = 30;
shop[0].salary = 3000; 
shop[1].fullname = "Bill Doesall";
shop[1].age = 25;
shop[1].salary = 2000;