What is structure with pointer in C ++

Compound type

Variables of the same type can be combined with arrays. In the real world, however, data of different types usually belong together. A car, for example, has a brand name and a type designation that can be accommodated as a character string. In contrast, the integer type is more suitable for mileage and performance. For the price, the type is ideal. Certain auto dealers might also require it. Everything together describes a car.


Perhaps you will object that there are more parts to a car. There are brake discs, turbochargers and windshield wipers. That is correct in reality. However, a program is only ever interested in certain properties that the programmer defines together with the customer. Our example might be enough for a small car dealer. A car rental company may not care about the value of the car at all, but want to note whether it is reserved for non-smokers. A workshop, on the other hand, could actually be interested in all parts. A program that manages the distribution of company vehicles may only be interested in the license plate. The result is a model of a car that contains certain components and neglects others, depending on what the program needs. Already in C there was a structure for such purposes that combines several variables into one. The key word for the designation of such composite variables is. This keyword is followed by the name of the new type. The components of the new structure are listed in the following block of curly brackets. These do not differ from the known variable definition. A semicolon concludes.


In order to model a car, a new variable type named is created, which is a composite of several elements. struct TAutoTyp // Define the type {char mark [MaxMarke]; char model [MaxModel]; long km; int kW; float price; }; // It's easy to forget the semicolon here!

Syntax description

The keyword introduces the type definition. It is followed by the name of the newly created type, here TAutoTyp. In the following pair of curly brackets, all components of the structure are listed one after the other. At the end there is a semicolon that even an experienced programmer forgets over and over again.

Variable definition

So we have the data type TAutoTyp created. It can be used in many ways like the data type. For example, you can create a tag of this data type. Yes, you can even define an array and a pointer of this data type. TAutoTyp MeinRostSammler; // Create variable TAutoTyp Fuhrpark [100]; // array of cars TAutoTyp * ParkhausKarte; // Pointer to a car

Element access

The variable My rust collector now contains all the information that is required in the declaration of TAutoTyp are set. In order to get from the variable to the individual parts, a period is attached to the variable name and the name of the component is attached to it. // Access the details MeinRostSammler.km = 128000; MeinRostSammler.kW = 25; MeinRostSammler.Preis = 25000.00;

Pointer sign

If you wanted to access a structure element via a pointer, you would have to refer to the asterisk and then access the element via the point. But since the point in front of the star is evaluated, you have to put a bracket around the star and the pointer name. TAutoTyp * ParkhausKarte = 0; // First of all no assignment ParkhausKarte = & MeinRostSammler; // Now it points to a car (* ParkhausKarte). Price = 12500; // Prize for MeinRostSammler

That might make sense, but it's neither elegant nor easy to remember. Fortunately, there is a nicer way of accessing structure elements via a pointer in C and C ++. For this purpose, a symbol is put together from the minus sign and the greater than sign that is reminiscent of an arrow.

Parking card-> price = 12500;


Structures are L values. So they can be on the left side of an assignment. Other structures of the same type can be assigned to them. The source variable is assigned to the target variable bit by bit. TAutoTyp MyNextCar, MyDreamAuto; MyNaechstesAuto = MyDreamAuto; Although the two structure variables are obviously the same after this operation, this cannot be checked simply by using the double equal sign. You can combine the type declaration and the variable definition for structures by entering the name of the variable directly after the curly brackets. struct // here no type is specified by name {char mark [MaxMarke]; char model [MaxModel]; long km; int kW; float price; } MyFirstCar, MyDreamAuto; Here in the example the variables My first car and My dream car defined at the same time as their structure. If variables of this structure are created in this way, a name does not necessarily have to be specified for the type. This means that it is of course not possible to create variables of this type at a later date.


Structures can also be initialized. As with the arrays, curly brackets are used for this purpose. Here, too, the values ​​are separated by commas. TAutoTyp JB = {"Aston Martin", "DB5", 12000, 90, 12.95}; TAutoTyp GWB = {0};


The structure has already been introduced with C and is the only way to define data networks there. In C ++, classes are normally used for this purpose, but they can do a lot more than the C structures. The keyword is still available in C ++ for reasons of compatibility and is translated as expected. However, a structure in C ++ can do a lot more. It is defined there in such a way that it corresponds to a class whose elements are publicly accessible.

At this point in the book is the syntax graph of struct (grafstruct).

The syntax graph for VarDef can be found on page (grafvardef). At this point there are variable definitions. Arrays, pointers or other structures are also permitted. As Vars variables may be enumerated from the structure. Pointers and arrays can be formed here. When defining multiple variables, they must be separated by commas.

Example: Bermuda

After the playing field has been modeled, the ships are to be reproduced here. Each ship has an x- and a y "= coordinate. Furthermore, you must be able to mark a ship when it has been found. So we also need a boolean variable named found. To put the ship together in this form, a structure is used. According to the game instructions, there are a total of four ships in the game, so an array of structures must be laid out. const int MaxShip = 4; struct tSchiff {int x; int y; bool found; }; tShip Ship [MaxSchiff];

The next step is to hide the ships. Of course, as with the lottery numbers, two ships cannot have the same position. As a result, the algorithm is very similar. Here you can see the complete program:

// Bermuda: Introduction of the structures #include using namespace std; #include const int X = 9; // horizontal expansion of the playing field const int Y = 7; // vertical extension of the playing field const int MaxSchiff = 4; // number of ships struct tSchiff {// replica of a ship int x; // position horizontally int y; // position vertical bool found; }; tShip Ship [MaxSchiff]; // Our fleet int main () {int i, j; // counter variables bool new number; // is a position found? srand (0); for (i = 0; i The data structures for the Bermuda program have now been created. The next step will be to structure the functionality.