Search Header Logo
Structures in C

Structures in C

Assessment

Presentation

Computers

12th Grade

Easy

Used 14+ times

FREE Resource

13 Slides • 9 Questions

1

Structures in C

Slide image

2

Lesson Objectives:

  • Define the term 'structure' as it relates to programming.

  • Differentiate between a structure and an array.

  • Implement structures in C programming.

3

What is a structure?

structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.

4

How does a structure differ from an array?

Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds.

5

Multiple Choice

A structures are similar to array in that they can store multiple data items.

1

True

2

False

6

Multiple Choice

A structures are similar to array in that they can store multiple data items.

1

True

2

False

7

Understanding structures

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 −

* Title

* Author

* Subject

* Book ID

8

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. The format of the struct statement is as follows −


struct [structure tag] {

------member definition;

------ member definition;

------ ...

------member definition;

} [one or more structure variables];


9

Example of Structure Definition

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 −

Slide image

10

Multiple Choice

How many members does struct Books contain?

1

1

2

0

3

3

4

4

11

Multiple Choice

How many data types are in struct Books?

1

1

2

2

3

3

4

4

12

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 the keyword struct to define variables of structure type. The following example shows how to use a structure in a program −

13

Slide image

14

Using typedef with struct

typedef is a keyword used in C language to assign alternative names to existing datatypes. Its mostly used with user defined datatypes, when names of the datatypes become slightly complicated to use in programs.

15

Using typedef with struct

typedef is a keyword used in C language to assign alternative names to existing datatypes. Its mostly used with user defined datatypes, when names of the datatypes become slightly complicated to use in programs.

16

Slide image

17

Open Ended

Which structure variable name are the members of struct Books being accessed using?

18

Open Ended

Respond to the following:


A car dealership desires to store the following information on vehicles:

*year

*brand

*model

*price


Define a struct 'Cars' to store the data.

19

Open Ended

Write a line of code giving struct Cars an alternate name.

20

Open Ended

Write a line of code to store the value 2020 in the year variable using the alternative struct name you declared.

21

Time to Code!!!

Open your C compiler and let's have some FUN :)

22

Open Ended

Your response here

Structures in C

Slide image

Show answer

Auto Play

Slide 1 / 22

SLIDE