Thus Far - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Thus Far

Description:

Price. Book. Make. Model. Year. Car. Name. ID. Age. Major ... float Price. Book. int Make. int Model. int Year. Car. string Name. string ID. unsigned Age ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 16
Provided by: rzh
Category:
Tags: far | thus

less

Transcript and Presenter's Notes

Title: Thus Far


1
Thus Far
  • Whenever we declare a variable, we specified its
    data type.
  • The data type helped us identify the type of
    information that a variable holds.
  • So far we have seen simple data types
  • int - represent whole numbers
  • float - represent numbers with decimal
    (fractional part),
  • char - represent characters
  • We have also seen arrays, that allow us to
    represent a collection of data elements of a
    specific type with a single variable name.
  • We accessed individual elements in the collection
    using the array subscript operator .

2
Structures
  • Structures in C allows us to represent a
    collection of variables (possibly of different
    types) under a single name.
  • It allows us to create record style data with
    various fields.
  • More generally, it allows us to model real-world
    entities.
  • An entity is something that has a distinct,
    seperate existence.
  • An entity has a set of attributes that describes
    it.
  • Any object in the real-world can be modeled as an
    entity.
  • A book is an entity that can be distinguished
    from say a car.

3
Examples
4
Examples Programming Specific
5
Structure Declaring
  • A collection of values (members) type
    declaration
  • struct struct_name
  • type1 data_member1
  • type2 data_member2
  • typeN data_memberN
  • Declare a structure variable instance
    declaration
  • struct struct_name instance_name

6
Examples Programming Specific
struct Student char Name50 char ID9
unsigned Age int Major char
Hometown struct Student you
7
Examples Programming Specific
struct Book char Title100 char
Author100 float price struct Book
bestSeller
8
Examples Programming Specific
struct Car int Make int Model int
Year struct Car carForSale
9
Examples Programming Specific
struct Country char Name100 char
Capital100 unsigned Population float
Longitude float Latitude struct Country
home
10
Examples Programming Specific
struct Date int Day char Month10 int
Year struct Date today
11
Declarations
  • Three ways

struct date int day char month10 int
year struct date today
typedef struct int day char month10 int
year date date today
struct int day char month10 int year
today
12
Initialization
  • struct
  • int day
  • char month10
  • int year
  • today 15, June, 2007

typedef struct int day char month10 int
year date date today 15, June, 2007
struct date int day char month10 int
year struct date today 15, June, 2007
13
How to use
.day
  • To access the members in the structure
  • specify the variable name, followed by a period
    and the member name
  • today.day 15
  • today.year 2007
  • today.month0J
  • today.month1u
  • today.month2n
  • today.month3e
  • today.month4\0
  • OR
  • today.day 15
  • today.year 2007
  • today.monthJune

.month
today
.year
14
How to use structure cont.
  • Structure variable can be passed as a parameter
    to a function.
  • Structure variable can be returned from a
    function
  • Can have arrays of structures, and structures
    containing arrays or other structures.
  • struct date list_of_days10
  • struct journalEntry
  • int location
  • struct date when
  • char entry1000

15
Scope
  • A structure type declaration can be local or
    global
  • if declared locally, it is only valid locally.

struct ONE int a float b void
main () struct ONE x int f()
struct ONE x
void main () struct ONE int a
float b struct ONE x int f()
struct ONE x
Write a Comment
User Comments (0)
About PowerShow.com