Engineering Problem Solving with C Fundamental Concepts - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Engineering Problem Solving with C Fundamental Concepts

Description:

struct Car Car1, Car2; Car1.year = 99; Car1.price = 18599.99; Car1.color = 'red' ... r2=mirror(r1); Etter/Ingber. Nested Structures ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 17
Provided by: csU66
Category:

less

Transcript and Presenter's Notes

Title: Engineering Problem Solving with C Fundamental Concepts


1
Engineering Problem Solving with C Fundamental
Concepts
  • Chapter 7
  • Structures

2
Structure Basics
  • A structure is a collection of data values,
    called data members, that form a single unit.
  • Unlike arrays, the data members can be of
    different types.

3
Structure Definition
  • struct name
  • variable declaration
  • variable declaration
  • .
  • .
  • the keyword struct announces that this is a
    structure definition

4
Example
  • struct node
  • int data
  • node link
  • the above defines a structure type
  • the name of the structure type (node) is called
    the structure tag
  • the identifiers declared inside the braces are
    called member names. Members can be declared to
    be of any valid C data type
  • the tag node may now be used just like
    predefined types int, char, etc

5
Declaring Structure Variables
  • struct node n1
  • struct node head
  • The structure n1 is a collection of smaller
    variables called member variables. The member
    variables are accessed using the dot (.)
    operator
  • head n1
  • n1.data 2
  • n1.link NULL

2
6
Initializing Structures
  • struct Rect
  • double x
  • double y
  • double width
  • double height
  • struct Rect r1 0,0,5,10

7
Practice!
Write the C statements necessary to output the
value of the structure r1 defined in the previous
slide.
8
Assignment operator
  • Assignment operator is defined for structure of
    the same type. (Compiler looks at tag, not
    composition.)
  • struct Car
  • char model, color
  • double price
  • int year
  • struct Car Car1, Car2
  • Car1.year 99
  • Car1.price 18599.99
  • Car1.color red
  • Car1.model Contour
  • / Copy all data from Car1 to Car2. /
  • Car2 Car1

9
Scope of a Structure
  • Member variables are local to the structure.
  • Member names are not known outside the structure.

10
Arrays of Structures
  • Arrays of structures may be declared in the same
    way as other C data types.
  • struct Car Ford20
  • Ford0 references first structure of Ford array.
  • Ford0.model Taurus

11
Structures as Arguments to Functions
  • When a structure is passed as an argument to a
    function, it is a call-by-value reference.
  • Changes made to the formal parameters to not
    change the argument.
  • A pointer to a structure may also be passed as an
    argument to a function.
  • Changes made to the formal parameters also change
    the argument.

12
Call by Value Example
Example struct simple int ival double
dval int main(void) void fun1(struct
simple s) struct simple s1 10,
1.5 fun1(s1) printf(i lf, s1.ival ,
s1.dval ) return 0 void fun1(struct simple
s) output?___________ s.ival s.dval

13
Pointers to Structures
  • When using pointers to access structure members,
    the
  • arrow operator is used.
  • Example
  • struct node
  • int data
  • struct node link
  • struct node nptr, n
  • nptr n
  • nptr-gtdata 2
  • nptr-gtlink NULL

14
Pointers to Structures
  • struct node
  • int data
  • struct node link
  • int main(void)
  • struct node nptr, n2, NULL
  • void fun1(struct node)
  • nptr n
  • fun1(nptr)
  • printf(i, n.data )
  • return 0
  • Output?___________________
  • void fun1(struct node n)

15
Structures as Return Values
  • struct Rect
  • double x,y,width, height
  • int main(void)
  • struct Rect mirror(struct Rect r)
  • struct Rect r1 0,0,5,10, r2
  • r2mirror(r1)
  • .
  • .

16
Nested Structures
  • Structure definitions may contain data members
    that are other structures
  • Example
  • struct Card
  • char suit
  • int rank
  • struct Deck
  • struct Card cards52
  • int next_card 0
Write a Comment
User Comments (0)
About PowerShow.com