0% found this document useful (0 votes)
28 views16 pages

Structures

This document provides an overview of programming fundamentals related to structures in C++. It defines a structure as a collection of variables of similar or dissimilar types related in nature. Structures allow storing multiple data types together, like storing the name, price, and number of pages for a book. It describes how to declare a structure type, declare structure variables, define elements, access elements using a dot operator, and create arrays of structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views16 pages

Structures

This document provides an overview of programming fundamentals related to structures in C++. It defines a structure as a collection of variables of similar or dissimilar types related in nature. Structures allow storing multiple data types together, like storing the name, price, and number of pages for a book. It describes how to declare a structure type, declare structure variables, define elements, access elements using a dot operator, and create arrays of structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Programming Fundamental

1
Today’s lecture outline
• Structures
• Declaring a structure
• Accessing structure elements
• How structure elements are stored
• Array of structure

2
Structure
• Variable can store single value of one type
• Array can store multiple values of similar type
• Structure can store multiple values of similar or dissimilar type
• Structures are a collection of variables related in nature, but not
necessarily in data type

Book Name Price Pages


Let us C 550.50 728
Kite runner 500.75 336

3
Why use structures
• Suppose we want to store data about a book
• You might want to store book’s
• name (a string),
• price (a float)
• number of pages (an int).
• If data about say 3 such books is to be stored, then we can follow two
approaches:
– Construct three arrays, one for storing names, second for storing prices and third
for number of pages
– Use of structure variables

4
Cont.
• Three separate array approaches
1 2 3 1 2 3 1 2 3

0 1 2 0 1 2 0 1 2
name[] price[] pages[]

• Structure variable
s1 s2 s3
name
price
pages

5
Three separate array approach

Go to program

6
Structure approach

Go to program
b1 b2 b3
name
price
pages 7
Structure definition

• Memory will not be allocated when you write structure definition


• This is just to tell the C++ compiler that what are the elements of a
variable of this structure
• This statement defines a new data type called struct book

8
Declaring structure variable

• This statement declare three variables b1, b2 and b3 of struct book


type.
• Now the memory will be allocated for these three variables
• These bytes are always in adjacent memory locations

b1 b2 b3
name
price
pages
9
Cont.
• Structure definition and declaration of structure variables can
be combined

Variable declaration Structure definition


Variable declaration
Definition

10
Variable initialization
• Structure variable can be initialize at declare time as well from
the input by user

11
Scope of structure
• Local scope
– If the structure is define inside any function, it will only be
accessible within that function
• Global scope
– If structure is define outside function, it will be accessible to
all the function in that program

Go to program

12
Accessing Structure Elements
• In arrays individual elements are accessed using a subscript
• A dot (.) operator is used with structure variable name to access
individual element of structure
• For example

b1.price = 100.75;
b1.pages = 450;
strcpy(b1.name, “let us c”);

13
Array of Structures

Go to program

0 1 2 3 4
b[5]

price pages name[10]


14
Additional feature
• The values of a structure variable can be assigned to another structure
variable of the same type using the assignment operator

15
Cont.
• One structure can be nested within another structure

16

You might also like