Using arrays in structures. Arrays of structures. Nested structures. Copying structures
Contents
- Question/Answer
- 1. How to declare a one-dimensional array of structures? Example
- 2. How to declare a two-dimensional array of structures? Example
- 3. An example of copying (assigning) one structure to another
- 4. An example of a structure that implements interface methods
- 5. What are the features of using arrays in structures? Example of using arrays in a structure
- 6. An example of using nested structures in programs
- Related topics
Search other websites:
1. How to declare a one-dimensional array of structures? Example
Below is the code that does the following:
- a structure of type Point describing the coordinates of a point on a plane is declared. The structure contains internal fields (variables x, y), one method of accessing variables, the properties of writing to the structure, and reading data from the structure;
- declares an array of MP from 10 instances (objects) of structures of type Point;
- demonstrates the use of an MP array in some method.
Declaring a structure of type Point:
// structure, describing a point on the coordinate plane struct Point { int x, y; public void SetXY(int nx, int ny) { x = nx; y = ny; } // property X public int X { get { return x; } set { x = value; } } // property Y public int Y { get { return y; } set { y = value; } } }
Declaring an MP array and using it in the code of some event handler (Windows Forms application).
// one-dimensional array of structures of type Point Point[] MP; // declaration of a variable of type "array Point" // allocate memory for the 10 structures of type Point MP = new Point[10]; // Reset the value of all points in the array MP for (int i = 0; i < 10; i++) MP[i].SetXY(0, 0); // use the SetXY() method of structure // Write some values to an array Point of points for (int i = 0; i < 10; i++) { // use the X and Y properties MP[i].X = i * 2; MP[i].Y = i + 1; }
⇑
2. How to declare a two-dimensional array of structures? Example
Below is an example of declaring and using a two-dimensional array of Point type (see previous paragraph).
// Declaring and using a two-dimensional array of structures of size 10 * 20 // 1. Allocate memory for the 10 strings Point[][] MP2 = new Point[10][]; // 2. In each line, allocate memory for 20 instances of structures for (int i=0; i<10; i++) MP2[i] = new Point[20]; // 3. Filling the fields of structures with arbitrary values for (int i = 0; i < 10; i++) for (int j = 0; j < 20; j++) { MP2[i][j].X = i + j * 3; MP2[i][j].Y = j - i * 2; }
⇑
3. An example of copying (assigning) one structure to another
Let the declaration of the Date structure defining the date in the calendar be given.
struct Date { public int day; // day public int month; // month public int year; // year public Date(int nday, int nmonth, int nyear) { day = nday; month = nmonth; year = nyear; } }
The following code demonstrates copying one structure to another.
// copying one structure to another Date St1; Date St2; St1.day = 14; St1.month = 1; St1.year = 1972; // copying the structures, structures are placed in different parts of memory St2 = St1; int d, m, y; d = St2.day; // d = 14 m = St2.month; // m = 1 y = St2.year; // y = 1972 St1.month = 5; // St1.month = 5; St2.month = 1;
As you can see from the example, the instances of the structures are located in different parts of memory.
⇑
4. An example of a structure that implements interface methods
As you know, you can implement interface methods in structures. The following example shows the use of interface methods in structures.
Let the IPoint interface and the Point type structure be given that have the same declaration
// The interface that will be used in the Point structure interface IPoint { // interface methods bool IsPointOnPoint(int x1, int y1, int x2, int y2); } // the Point structure inherits the interface struct Point : IPoint { public int x, y; // an explicitly defined constructor public Point(int nx, int ny) { x = nx; y = ny; } // Implementation of the interface method in the structure public bool IsPointOnPoint(int x1, int y1, int x2, int y2) { bool f; f = false; if ((x1 == x2) && (y1 == y2)) f = true; return f; } }
The interface describes the method IsPointOnPoint (), which can be implemented in the structure of Point. The Point structure inherits the IPoint interface. The IsPointOnPoint() method determines whether the points have the same x, y coordinates.
The following code shows how to use a Point type structure and call the IsPointOnPoint() interface method.
// Using a structure that inherits the interface Point P; bool res; P.x = P.y = 0; // call the IsPointOnPoint () interface method implemented in the Point structure res = P.IsPointOnPoint(3, 5, 6, 6); // res = false res = P.IsPointOnPoint(3, 7, 3, 7); // res = true
⇑
5. What are the features of using arrays in structures? Example of using arrays in a structure
If an array is declared in the structure, memory must always be allocated to it using the new operator.
Example. Let the structure Cars, which describes the park of cars, be given. The structure declares several arrays that determine the characteristics of cars.
// structure that describes a car fleet struct Cars { public int n; // number of cars public string[] model; // car model public int[] year; // year of issue public float[] engine; // engine power public string[] country; // Manufacturer country public bool[] on_the_run; // true - the car on the move }
The following code demonstrates the use of arrays in the structure
// the use of arrays in structures Cars C; // declaration of a structured variable // forming an array of 10 cars C.n = 10; // allocating memory for arrays in structures - mandatory C.model = new string[C.n]; C.year = new int[C.n]; C.engine = new float[C.n]; C.country = new string[C.n]; C.on_the_run = new bool[C.n]; // Array filling with arbitrary values for (int i = 0; i < C.n; i++) { C.model[i] = "Model - " + (i + 1).ToString(); C.year[i] = 2000 + i; C.engine[i] = 1.2f + 0.1f * i; C.country[i] = "Country - " + (i + 1).ToString(); C.on_the_run[i] = ((i % 2) == 0) ? true : false; // true - pair, false - odd }
⇑
6. An example of using nested structures in programs
The example declares three structures named Date, WorkerName, Worker. The Worker structure describes employee information and contains two nested Date and WorkerName structures. Also, the Worker structure contains an array of float-type numbers.
// nested structures // structure that describes the date struct Date { public int day; // day public int month; // month public int year; // year } // A structure that describes the name of the employee struct WorkerName { public string name; // name public string surname; // surname public string patronymic; // patronymic } // structure that describes information about the employee struct Worker { public WorkerName Name; // last name and first name public Date BirthDate; // Date of Birth public string code; // identification code public int rank; // employee category public float[] salary; // accrued monthly salary }
Using the Worker structure in another program code
// array of 5 structures like Worker Worker[] W = new Worker[5]; // allocating memory for the salary field, which is an array - necessarily! for (int i=0; i<5; i++) W[i].salary = new float[12]; // filling the array W for (int i = 0; i < 5; i++) { W[i].Name.name = "Name of worker - " + i.ToString(); W[i].Name.surname = "Surname of worker - " + i.ToString(); W[i].Name.patronymic = "Patronymic of worker - " + i.ToString(); W[i].BirthDate.day = 1; W[i].BirthDate.month = 1; W[i].BirthDate.year = 1972; W[i].code = "1234567890"; W[i].rank = i + 2; for (int j = 0; j < 12; j++) W[i].salary[j] = (float)(i * 500 + 220.00); }
⇑
Related topics
- Structures. General concepts. Type of structure. Structural variable. Modifiers of access to structured variables. Examples of using the structures
- Structures and methods. Initialization of structures. Constructors in structures. Properties in structures
- The concept of class. Class definition. Object of class. Constructor of the class. Destructor. “Garbage collection”. Keyword this
- Interfaces. The use of interfaces in C# programs