What is a structure? Write a structure specification in C++ for the record
given below : 4
Code : A string of 5 charac ters (in clud ing Null charac ter)
Desc : A string of 25 char ac ters (in clud ing Null charac ter)
Cost : In te ger
Mar gin : In te ger
Call this structure ITEM.
Answers
Answered by
1
C++ arrays allow you to define variables that combine several data items of the same kind, but structure is another user defined data type which allows you to combine data items of different kinds.
Structures are used to represent a record, suppose you want to keep track of your books in a library. You might want to track the following attributes about each book −
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member, for your program. The format of the struct statement is this −
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional. Here is the way you would declare the Book structure −
struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use structkeyword to define variables of structure type. Following is the example to explain usage of structure −
Structures are used to represent a record, suppose you want to keep track of your books in a library. You might want to track the following attributes about each book −
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member, for your program. The format of the struct statement is this −
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional. Here is the way you would declare the Book structure −
struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use structkeyword to define variables of structure type. Following is the example to explain usage of structure −
Answered by
0
A structure is an arrangement and organization of interrelated elements in a material object or system, or the object or system so organized. Material structures include man-made objects such as buildings and machines and natural objects such as biological organisms, minerals and chemicals.
Missheartkiller ✨✌️
Similar questions