Title: Thus Far
1Thus 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 .
2Structures
- 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.
3Examples
4Examples Programming Specific
5Structure 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
6Examples Programming Specific
struct Student char Name50 char ID9
unsigned Age int Major char
Hometown struct Student you
7Examples Programming Specific
struct Book char Title100 char
Author100 float price struct Book
bestSeller
8Examples Programming Specific
struct Car int Make int Model int
Year struct Car carForSale
9Examples Programming Specific
struct Country char Name100 char
Capital100 unsigned Population float
Longitude float Latitude struct Country
home
10Examples Programming Specific
struct Date int Day char Month10 int
Year struct Date today
11Declarations
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
12Initialization
- 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
13How 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
14How 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
-
15Scope
- 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