SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Downloaden Sie, um offline zu lesen
Page 1 of 6

Structures
What is a Structure?
Structure is a collection of variables under a single name. Variables can be of
any type: int, float, char etc. The main difference between structure and array is
that arrays are collections of the same data type and structure is a collection of
variables under a single name.

How to declare and create a Structure
Declaring a Structure:
The structure is declared by using the keyword struct followed by structure name,
also called a tag. Then the structure members (variables) are defined with their
type and variable names inside the open and close braces { and }. Finally, the
closed braces end with a semicolon denoted as ; following the statement. The
above structure declaration is also called a Structure Specifier.
Example:
Three variables: custnum of type int, salary of type int, commission of type float
are structure members and the structure name is Customer. This structure is
declared as follows:

In the above example, it is seen that variables of different types such as int and
float
are
grouped
in
a
single
structure
name
Customer.
Arrays behave in the same way, declaring structures does not mean that memory
is allocated. Structure declaration gives a skeleton or template for the structure.
After declaring the structure, the next step is to define a structure variable.

Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 2 of 6

How to declare Structure Variable?
This is similar to variable declaration. For variable declaration, data type is
defined followed by variable name. For structure variable declaration, the data
type is the name of the structure followed by the structure variable name.
In the above example, structure variable cust1 is defined as:

What happens when this is defined? When structure is defined, it allocates or
reserves space in memory. The memory space allocated will be cumulative of all
defined structure members. In the above example, there are 3 structure
members: custnum, salary and commission. Of these, two are of type in and one
is of type float. If integer space allocated by a system is 2 bytes and float four
bytes the above would allo9acter 2bytes for custnum, 2 bytes for salary and 4
bytes for commission.

How to access structure members in C++?
To access structure members, the operator used is the dot operator denoted by
(.). The dot operator for accessing structure members is used thusly:

structure variable name.member name
For example:
A programmer wants to assign 2000 for the structure member salary in the above
example of structure Customer with structure variable cust1 this is written as:

Nested Structures
A structure can be nested inside another structure.
Stuct addr
{
int houseno;
char area[26];
Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 3 of 6
char city[26];
char state[26];
};
struct emp
{
int empno;
char name[26];
char desig[16];
addr address;
float basic;
};
emp worker ;
The structure emp h as been defined having several elements including a
structure address also. The elements address(of structure emp) is itself a
structure of type addr. While defining such structures, just make sure that innder
structure one defined before outer structure one defined before outer structures.

Structure and Arrays
The structure and the array both are c++ derived data types. While arrays are
collections of analogous elements, structures assembles dissimilar elements
under one roof.
Array and structure both combined together to form complex data objects. There
may be structures contained within an array; also there array element of a
structure.
Array of Structure
To declare 100- elements array of structure of type addr
addr mem_addr[100];
To create an array having structures emp type
Emp sales_emp[100];
Above declaration to create an array sales_emp to store 100 structures of emp
type.

Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 4 of 6

Array Within Structures
A structure elements my be simple or complex . A complex structure may itself
be a structure or any array. When a structure elements happens to be an array it
is treated in the same way as array are treated. The only additional things to be
kept in mind is that to access it, its structure name followed by a (.) and the name
is to be given. For example consider structure ;

Struct student
{
Int rollno;
char name[21];
float marks[5]; //array of 5 floats
};
students learner;
The above declared statement variable learner is of structure type student that
contains an elements which is a n array of 5 floats to store marks of a students in
5 different subjects.

Passing Structures to Function
If you have a structure local to a function and you need to pass its values to
another function , then it can be achieved in two ways : (i) by passing individual
structure elements , and (ii) by passing the entire structure . Both the ways cab
be achieved by call by value as well as by call by reference method pa passing
variables.
Passing Structure Elements to Functions
When an elements of a structure is passed to a function , you are actually
passing the value of that element to the function Therefore , it just like passing a
simple variable. Consider following structure
Struct date {
short day;
shot month;
short year;
} Bdate;
`
Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 5 of 6
Passing entire structure to Functions
Passing entire structure makes the most sense when the structure is relatively
compact. The entire structure can be passed to the function both ways by value
and by reference. Passing by value is useful when the original values are not
to be changed and passing by reference is useful when original values are to
be changed.
User Defined Data Types
C++ allows you to define explicitly new data type name by using the keyword
typedef doses not actually create a new data class, rather it defines a new
name for an exiting type. This can increase the potablity of a program as only the
typedef statements would have to be changed . Using typedef can also aid in
self- documenting your code by allowing ddescriptive name for the standard data
type. The syntax of the typedef statement is
Typedef type name ;

Use of typedef for declaring structures
The typedef function is used to declare the alias name for the structure type.
typedef structure result
{
} progress;
results report1;
progress report2
in this above program segment the usage of typedef defines a structure result
and an alias name of the same structure as progress.
The two structures report1 and report2 are declared accordingly of the same
type as result and progress are now alternative nomenclature for each other.

Enumerated data types
It is another way of defining data types. It includes all the probables list of values
that a data type can take. It has the following format for declaration;
Enum struc-name {v1 v2 ,v3, v3,v4…..v n }

Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 6 of 6

Practice Question Paper
1. What is a structure?
2. Define arrays of structures?
3. Differentiate between the following using examples
a. Simple structure
b. multiple structure
4. Explain the following with examples
a. Declaration of a structure
b. Definition of a structure variable
c. Accessing of a structure member
d. Initialization of a member within structures.
e. Arrays of structures.
f. Passing structures to functions
g. Use defined structure
6. Explain the usage of type def for declaring structure .
7.What are enumerated data type?
8. What are symbolic constants?
9. Define a structure for length and breadth of a rectangle of type float and refer
the same as rect.
10. Write a statement to assign a variable measure to length member of rect
structure variable.
11. The enumerated type is considered as an integer type . Comment on the
factual concern of the statement.
12. Write a program segment to declared and initialize an array of four structure
with name of the student age and marks in three subjects.

Prepared By Sumit Kumar Gupta, PGT Computer Science

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Structure c
Structure cStructure c
Structure c
 
Structure in C
Structure in CStructure in C
Structure in C
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
structure and union
structure and unionstructure and union
structure and union
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
 
Function in C
Function in CFunction in C
Function in C
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Type conversion
Type conversionType conversion
Type conversion
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in C
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
C functions
C functionsC functions
C functions
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 

Andere mochten auch

C++ revision tour
C++ revision tourC++ revision tour
C++ revision tourSwarup Boro
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSwarup Boro
 
Spider Man Takes A Day Off
Spider Man Takes A Day OffSpider Man Takes A Day Off
Spider Man Takes A Day Offchrisregan501
 
Railway reservation
Railway reservationRailway reservation
Railway reservationSwarup Boro
 
Hoax Chris Regan
Hoax Chris ReganHoax Chris Regan
Hoax Chris Reganchrisregan501
 
Hoax Chris Regan
Hoax Chris ReganHoax Chris Regan
Hoax Chris Reganchrisregan501
 
Program to sort array using insertion sort
Program to sort array using insertion sortProgram to sort array using insertion sort
Program to sort array using insertion sortSwarup Boro
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Boro
 
EPSON PROJECTOR EB X14
EPSON PROJECTOR EB X14EPSON PROJECTOR EB X14
EPSON PROJECTOR EB X14Vyankatesh Dalvi
 
Iowa, anticipo de Clinton contra Rubio en otoño, de Alexandre Muns - Cinco Días
Iowa, anticipo de Clinton contra Rubio en otoño, de Alexandre Muns - Cinco DíasIowa, anticipo de Clinton contra Rubio en otoño, de Alexandre Muns - Cinco Días
Iowa, anticipo de Clinton contra Rubio en otoño, de Alexandre Muns - Cinco DíasEAE Business School
 
Compostagem
CompostagemCompostagem
CompostagemEliana Neto
 
Custom Company Stores
Custom Company StoresCustom Company Stores
Custom Company StoresRhonda Aicklen
 

Andere mochten auch (20)

Classes
ClassesClasses
Classes
 
Pointers
PointersPointers
Pointers
 
Boolean
BooleanBoolean
Boolean
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Spider Man Takes A Day Off
Spider Man Takes A Day OffSpider Man Takes A Day Off
Spider Man Takes A Day Off
 
Railway reservation
Railway reservationRailway reservation
Railway reservation
 
Hoax Chris Regan
Hoax Chris ReganHoax Chris Regan
Hoax Chris Regan
 
Hoax Chris Regan
Hoax Chris ReganHoax Chris Regan
Hoax Chris Regan
 
Program to sort array using insertion sort
Program to sort array using insertion sortProgram to sort array using insertion sort
Program to sort array using insertion sort
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Functions
FunctionsFunctions
Functions
 
EPSON PROJECTOR EB X14
EPSON PROJECTOR EB X14EPSON PROJECTOR EB X14
EPSON PROJECTOR EB X14
 
Iowa, anticipo de Clinton contra Rubio en otoño, de Alexandre Muns - Cinco Días
Iowa, anticipo de Clinton contra Rubio en otoño, de Alexandre Muns - Cinco DíasIowa, anticipo de Clinton contra Rubio en otoño, de Alexandre Muns - Cinco Días
Iowa, anticipo de Clinton contra Rubio en otoño, de Alexandre Muns - Cinco Días
 
Compostagem
CompostagemCompostagem
Compostagem
 
Muhammad Hassan
Muhammad HassanMuhammad Hassan
Muhammad Hassan
 
Custom Company Stores
Custom Company StoresCustom Company Stores
Custom Company Stores
 
Coffee
CoffeeCoffee
Coffee
 
James Ayeni CV
James Ayeni CVJames Ayeni CV
James Ayeni CV
 
Panneerselvam
PanneerselvamPanneerselvam
Panneerselvam
 

Ă„hnlich wie Structures in c++

2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++Jeff TUYISHIME
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structuresKrishna Nanda
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languagetanmaymodi4
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languageTanmay Modi
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qbaSowri Rajan
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9patcha535
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.pptParinayWadhwa
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures topu93
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture topu93
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And UnionsDhrumil Patel
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and UnionsDhrumil Patel
 

Ă„hnlich wie Structures in c++ (20)

Structures in c++
Structures in c++Structures in c++
Structures in c++
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Programming in C
Programming in CProgramming in C
Programming in C
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Structure.pptx
Structure.pptxStructure.pptx
Structure.pptx
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Unit 3
Unit 3Unit 3
Unit 3
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 

Mehr von Swarup Boro

Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Swarup Boro
 
Program: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsProgram: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsSwarup Boro
 
Array using recursion
Array using recursionArray using recursion
Array using recursionSwarup Boro
 
Binary addition using class concept in c++
Binary addition using class concept in c++Binary addition using class concept in c++
Binary addition using class concept in c++Swarup Boro
 
Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids                                 Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids Swarup Boro
 
Program using function overloading
Program using function overloadingProgram using function overloading
Program using function overloadingSwarup Boro
 
Program to find the avg of two numbers
Program to find the avg of two numbersProgram to find the avg of two numbers
Program to find the avg of two numbersSwarup Boro
 
Program to find factorial of a number
Program to find factorial of a numberProgram to find factorial of a number
Program to find factorial of a numberSwarup Boro
 
Canteen management program
Canteen management programCanteen management program
Canteen management programSwarup Boro
 
C++ program using class
C++ program using classC++ program using class
C++ program using classSwarup Boro
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Boro
 
Computer communication and networks
Computer communication and networks Computer communication and networks
Computer communication and networks Swarup Boro
 

Mehr von Swarup Boro (16)

Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++
 
Program: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsProgram: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 students
 
Array using recursion
Array using recursionArray using recursion
Array using recursion
 
Binary addition using class concept in c++
Binary addition using class concept in c++Binary addition using class concept in c++
Binary addition using class concept in c++
 
Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids                                 Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids
 
Program using function overloading
Program using function overloadingProgram using function overloading
Program using function overloading
 
Program to find the avg of two numbers
Program to find the avg of two numbersProgram to find the avg of two numbers
Program to find the avg of two numbers
 
Program to find factorial of a number
Program to find factorial of a numberProgram to find factorial of a number
Program to find factorial of a number
 
Canteen management program
Canteen management programCanteen management program
Canteen management program
 
C++ program using class
C++ program using classC++ program using class
C++ program using class
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Queue
QueueQueue
Queue
 
Stack
StackStack
Stack
 
Computer communication and networks
Computer communication and networks Computer communication and networks
Computer communication and networks
 
2D Array
2D Array2D Array
2D Array
 
1D Array
1D Array1D Array
1D Array
 

KĂĽrzlich hochgeladen

AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 

KĂĽrzlich hochgeladen (20)

AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 

Structures in c++

  • 1. Page 1 of 6 Structures What is a Structure? Structure is a collection of variables under a single name. Variables can be of any type: int, float, char etc. The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name. How to declare and create a Structure Declaring a Structure: The structure is declared by using the keyword struct followed by structure name, also called a tag. Then the structure members (variables) are defined with their type and variable names inside the open and close braces { and }. Finally, the closed braces end with a semicolon denoted as ; following the statement. The above structure declaration is also called a Structure Specifier. Example: Three variables: custnum of type int, salary of type int, commission of type float are structure members and the structure name is Customer. This structure is declared as follows: In the above example, it is seen that variables of different types such as int and float are grouped in a single structure name Customer. Arrays behave in the same way, declaring structures does not mean that memory is allocated. Structure declaration gives a skeleton or template for the structure. After declaring the structure, the next step is to define a structure variable. Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 2. Page 2 of 6 How to declare Structure Variable? This is similar to variable declaration. For variable declaration, data type is defined followed by variable name. For structure variable declaration, the data type is the name of the structure followed by the structure variable name. In the above example, structure variable cust1 is defined as: What happens when this is defined? When structure is defined, it allocates or reserves space in memory. The memory space allocated will be cumulative of all defined structure members. In the above example, there are 3 structure members: custnum, salary and commission. Of these, two are of type in and one is of type float. If integer space allocated by a system is 2 bytes and float four bytes the above would allo9acter 2bytes for custnum, 2 bytes for salary and 4 bytes for commission. How to access structure members in C++? To access structure members, the operator used is the dot operator denoted by (.). The dot operator for accessing structure members is used thusly: structure variable name.member name For example: A programmer wants to assign 2000 for the structure member salary in the above example of structure Customer with structure variable cust1 this is written as: Nested Structures A structure can be nested inside another structure. Stuct addr { int houseno; char area[26]; Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 3. Page 3 of 6 char city[26]; char state[26]; }; struct emp { int empno; char name[26]; char desig[16]; addr address; float basic; }; emp worker ; The structure emp h as been defined having several elements including a structure address also. The elements address(of structure emp) is itself a structure of type addr. While defining such structures, just make sure that innder structure one defined before outer structure one defined before outer structures. Structure and Arrays The structure and the array both are c++ derived data types. While arrays are collections of analogous elements, structures assembles dissimilar elements under one roof. Array and structure both combined together to form complex data objects. There may be structures contained within an array; also there array element of a structure. Array of Structure To declare 100- elements array of structure of type addr addr mem_addr[100]; To create an array having structures emp type Emp sales_emp[100]; Above declaration to create an array sales_emp to store 100 structures of emp type. Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 4. Page 4 of 6 Array Within Structures A structure elements my be simple or complex . A complex structure may itself be a structure or any array. When a structure elements happens to be an array it is treated in the same way as array are treated. The only additional things to be kept in mind is that to access it, its structure name followed by a (.) and the name is to be given. For example consider structure ; Struct student { Int rollno; char name[21]; float marks[5]; //array of 5 floats }; students learner; The above declared statement variable learner is of structure type student that contains an elements which is a n array of 5 floats to store marks of a students in 5 different subjects. Passing Structures to Function If you have a structure local to a function and you need to pass its values to another function , then it can be achieved in two ways : (i) by passing individual structure elements , and (ii) by passing the entire structure . Both the ways cab be achieved by call by value as well as by call by reference method pa passing variables. Passing Structure Elements to Functions When an elements of a structure is passed to a function , you are actually passing the value of that element to the function Therefore , it just like passing a simple variable. Consider following structure Struct date { short day; shot month; short year; } Bdate; ` Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 5. Page 5 of 6 Passing entire structure to Functions Passing entire structure makes the most sense when the structure is relatively compact. The entire structure can be passed to the function both ways by value and by reference. Passing by value is useful when the original values are not to be changed and passing by reference is useful when original values are to be changed. User Defined Data Types C++ allows you to define explicitly new data type name by using the keyword typedef doses not actually create a new data class, rather it defines a new name for an exiting type. This can increase the potablity of a program as only the typedef statements would have to be changed . Using typedef can also aid in self- documenting your code by allowing ddescriptive name for the standard data type. The syntax of the typedef statement is Typedef type name ; Use of typedef for declaring structures The typedef function is used to declare the alias name for the structure type. typedef structure result { } progress; results report1; progress report2 in this above program segment the usage of typedef defines a structure result and an alias name of the same structure as progress. The two structures report1 and report2 are declared accordingly of the same type as result and progress are now alternative nomenclature for each other. Enumerated data types It is another way of defining data types. It includes all the probables list of values that a data type can take. It has the following format for declaration; Enum struc-name {v1 v2 ,v3, v3,v4…..v n } Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 6. Page 6 of 6 Practice Question Paper 1. What is a structure? 2. Define arrays of structures? 3. Differentiate between the following using examples a. Simple structure b. multiple structure 4. Explain the following with examples a. Declaration of a structure b. Definition of a structure variable c. Accessing of a structure member d. Initialization of a member within structures. e. Arrays of structures. f. Passing structures to functions g. Use defined structure 6. Explain the usage of type def for declaring structure . 7.What are enumerated data type? 8. What are symbolic constants? 9. Define a structure for length and breadth of a rectangle of type float and refer the same as rect. 10. Write a statement to assign a variable measure to length member of rect structure variable. 11. The enumerated type is considered as an integer type . Comment on the factual concern of the statement. 12. Write a program segment to declared and initialize an array of four structure with name of the student age and marks in three subjects. Prepared By Sumit Kumar Gupta, PGT Computer Science