You get a bonus - 1 coin for daily activity. Now you have 1 coin

Syntax: structures in C and C ++

Lecture



Structure declaration

A structure is an aggregate data type, since it can contain elements of different types. The syntax for declaring a structure in C ++ is different from C. Although the C version remains correct for C ++. It turns out that in C ++ it is possible to use two styles of the declaration of structures, and in the C language - only one. We look at the syntax of the structure declaration in C ++:

1

2

3

4

5

struct Name

{

type atrib;

// остальные элементы структуры

} structVar1, structVar2, ...;

Where,

  • struct is the keyword that starts the structure definition
  • Name - the name of the structure
  • type - the data type of the structure element
  • atrib - structure element
  • structVar1-2 - structure variables

A structure declaration must always begin with the struct keyword. It is not necessary that the structure has a name, but then such a structure must necessarily have structural variables declared between the closing brace and the semicolon, line 5. Bracelets must be present in the declaration of the structure, they frame the body of the structure in which its attributes are declared ( elements), line 3. Structural variables, when declaring a structure, optional, line 5.

Since a structure is a data type, in order to use this data type, it is necessary to declare a structure variable, and instead of a data type, specify the name of the structure.

1

struct_name structVariable;

The syntax of the structure declaration in the C language:

1

2

3

4

5

6

typedef struct name

{

type atrib1;

type atrib2;

// остальные элементы структуры...

} newStructName structVar;

The syntax for declaring a structure in the C language involves two options. First, omit the typedef keyword, while the name newStructName also not used, and the name of the structure, then it is necessary to use structural variables - structVar , line 6, when declaring the structure. structVar an example:

1

struct name structVar;

Or you can use typedef to declare an alias of the structure newStructName , alias:

1

newStructName structVar;

In any case, if you want to declare a pointer to a structure within a structure, you must use the first syntax:

1

struct name *struct_instance; // указатель на структуру

Declaring a pointer to a structure

The syntax for declaring a pointer to a structure in C is ambiguous. In C, if you do not use typedef when defining a structure, then it is imperative to use structural variables between the closing curly bracket and the semicolon.
In C ++, this is not required. To declare a pointer to a structure, in C ++ you simply put the pointer symbol - * in front of the name of a structural variable.

In C ++:

1

structName *structVar; // указатель на структуру structName

In C:

  newStructName * structVar;  // newStructName must be declared with typedef 

or so, also for SI:

1

struct name *structVar;

Access to structure elements

Access to structure elements is as easy as using the dot character. Suppose that we have a structural variable named car and it has an element named speed , to which we will now access:

1

car.speed;

Note: this way of accessing structure elements works only when the structure is not a pointer to the structure.

Access to structure pointer elements

To get access to the elements of the structure, through the pointer to the structure, instead of the “dot” operator, use the arrow operator -> :

1

carPtr->speed;

PS: I represent a good selection of GPS navigator programs for android to all owners of Android-smartphones. The list contains about 20 software products, you can download and install any on your device. All programs are absolutely free.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Algorithmization and programming. Structural programming. C language

Terms: Algorithmization and programming. Structural programming. C language